Quick Action Demo

15

Description:
This example of will show how we can generate a Action list or a popup window on a button press.

Algorithm:
1.) Create a new project by File-> New -> Android Project name it QuickActionDemo.
2.) You will see some default code into your main.xml, strings.xml and android manifest file.
3.) Now write following code into your main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

	<TextView
		android:layout_width="fill_parent"
    	android:layout_height="wrap_content"    	
    	android:gravity="center"
    	android:textSize="17dp"
    	android:textStyle="bold"
    	android:text="Popup Window Action Demo"/>
    	
	<Button
		android:id="@+id/btn2"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:padding="30dp"
		android:text="Button" />
    	 
</LinearLayout>

4.) Create ActionItem.java and write following code:

package com.org.QuickActionActivity;

import android.graphics.drawable.Drawable;
import android.graphics.Bitmap;

public class ActionItem {
	private Drawable icon;
	private Bitmap thumb;
	private String title;
	private boolean selected;
	
	public ActionItem() {}
	public ActionItem(Drawable icon) {
		this.icon = icon;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTitle() {
		return this.title;
	}
	public void setIcon(Drawable icon) {
		this.icon = icon;
	}
	public Drawable getIcon() {
		return this.icon;
	}
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	public boolean isSelected() {
		return this.selected;
	}
	public void setThumb(Bitmap thumb) {
		this.thumb = thumb;
	}
	public Bitmap getThumb() {
		return this.thumb;
	}
}

5.) Create NewQAAdapter.java and write following:

package com.org.QuickActionActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.content.Context;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewQAAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private String[] data;
	
	public NewQAAdapter(Context context) { 
		mInflater = LayoutInflater.from(context);
	}

	public void setData(String[] data) {
		this.data = data;
	}
	
	@Override
	public int getCount() {
		return data.length;
	}

	@Override
	public Object getItem(int item) {
		return data[item];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		
		if (convertView == null) {
			convertView	= mInflater.inflate(R.layout.list, null);
			
			holder 		= new ViewHolder();
			
			holder.mTitleText	= (TextView) convertView.findViewById(R.id.t_name);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.mTitleText.setText(data[position]);
		
		return convertView;
	}

	static class ViewHolder {
		TextView mTitleText;
	}
}

6.) Click here to download full code content and other src and res files.

Steps:

1.) Create a project named QuickActionDemo and set the information as stated in the image.

Build Target: Android 2.1
Application Name: QuickActionDemo
Package Name: com.org. QuickActionDemo
Activity Name: QuickActionDemo
Min SDK Version: 7

2.) Open QuickActionDemo.java file and write following code there:

package com.org.QuickActionActivity;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class QuickActionActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
		
		//Add action item
        ActionItem addAction = new ActionItem();
		
		addAction.setTitle("Add");
		addAction.setIcon(getResources().getDrawable(R.drawable.ic_add));

		//Accept action item
		ActionItem accAction = new ActionItem();
		
		accAction.setTitle("Accept");
		accAction.setIcon(getResources().getDrawable(R.drawable.ic_accept));
		
		//Upload action item
		ActionItem upAction = new ActionItem();
		
		upAction.setTitle("Upload");
		upAction.setIcon(getResources().getDrawable(R.drawable.ic_up));
		
		final QuickAction mQuickAction 	= new QuickAction(this);
		
		mQuickAction.addActionItem(addAction);
		mQuickAction.addActionItem(accAction);
		mQuickAction.addActionItem(upAction);
		
		//setup the action item click listener
		mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {			
			@Override
			public void onItemClick(int pos) {
				
				if (pos == 0) { //Add item selected
					Toast.makeText(QuickActionActivity.this, "Add item selected", Toast.LENGTH_SHORT).show();
				} else if (pos == 1) { //Accept item selected
					Toast.makeText(QuickActionActivity.this, "Accept item selected", Toast.LENGTH_SHORT).show();
				} else if (pos == 2) { //Upload item selected
					Toast.makeText(QuickActionActivity.this, "Upload items selected", Toast.LENGTH_SHORT).show();
				}	
			}
		});
		Button btn2 = (Button) this.findViewById(R.id.btn2);
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mQuickAction.show(v);
				mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
			}
		});
	}
}       

3.) Compile and build the project.
4.) Run on 2.1 simulator for the output.

Output

Did you enjoy this article?
Share
the
Love
Get Free Updates

15 Responses

  1. Savin says:

    thanks so much !!

  2. rajesh sanandiya says:

    i want to learn SQLite in android
    you can help me about SQLite with Example source code

    thank you

  3. TARUN says:

    IT’S GOOD

  4. TARUN says:

    IT\’S GOOD

  5. Steven says:

    This is an awesome tutorial!!!

  6. Ramkumar says:

    Where is the reference to quickaction is done?

  7. Thankyou very much. This was very helpful

  8. Shashant D. says:

    Thankyou so much….was able to recreate it, but i want it to look like a grid..say in 2 * 2 matrix form….can you plz tell me how to do that..thanx in advance!! this was really helpful

  9. Paul Z. says:

    Hey you, why in this world didn’t you include the layout for the buttons here? You are referencing them in your code, so you were ought to include them into this so-called ‘tutorial’.

  10. sacca says:

    Hai, this info is very nice. i’m blessed to get this kind of info from you :=)

  11. hoangnguyen says:

    Thank you for the tutorial. I done but not working. Please send me your source code. Thank you very much.
    Email: nguyenhoangnhung92@gmail.com

  12. haroun says:

    thanks.

    my problem is solved by this tuto :)

  13. Ihor K. says:

    Thaks a lot!! Awesome article!

  14. mayank says:

    Thankyou so much….was able to recreate it, but i want it to look like a grid..say in 2 * 2 matrix form….can you plz tell me how to do that..thanx in advance!! this was really helpful

  15. mayank says:

    can you plz send me gridview code plz

Leave a Reply


× 1 = six

Proudly designed by Theme Junkie.