Quick Action Demo

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

About the Author

Leave a Reply 15 comments

Savin - September 30, 2011 Reply

thanks so much !!

rajesh sanandiya - October 3, 2011 Reply

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

thank you

TARUN - December 19, 2011 Reply

IT’S GOOD

TARUN - December 19, 2011 Reply

IT’S GOOD

Steven - June 13, 2012 Reply

This is an awesome tutorial!!!

Ramkumar - June 23, 2012 Reply

Where is the reference to quickaction is done?

Mathew Gathoka - August 6, 2012 Reply

Thankyou very much. This was very helpful

Shashant D. - October 16, 2012 Reply

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

Paul Z. - November 27, 2012 Reply

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’.

sacca - February 4, 2013 Reply

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

haroun - June 13, 2013 Reply

thanks.

my problem is solved by this tuto :)

Ihor K. - October 26, 2013 Reply

Thaks a lot!! Awesome article!

mayank - October 31, 2013 Reply

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

mayank - October 31, 2013 Reply

can you plz send me gridview code plz

Leave a Reply: