Quick Action Demo

2

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

2 Responses

  1. Thankyou very much. This was very helpful

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

Leave a Reply


eight + 8 =

Proudly designed by Theme Junkie.