Click here to Skip to main content
15,891,248 members
Articles / Mobile Apps / Android

Quick action pattern in Android and simple implemention.

Rate me:
Please Sign up or sign in to vote.
4.89/5 (26 votes)
8 Jan 2013CPOL4 min read 113.1K   3.7K   107  
Explorer Quick action pattern in Adroid and implement some simple demos.
package vn.com.enclaveit.phatbeo.quickaction;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Add action item
		ActionItem addAction = new ActionItem();

		addAction.setTitle("Phone");
		addAction.setIcon(getResources().getDrawable(R.drawable.phone));

		// Accept action item
		ActionItem accAction = new ActionItem();

		accAction.setTitle("Gmail");
		accAction.setIcon(getResources().getDrawable(R.drawable.gmail));

		// Upload action item
		ActionItem upAction = new ActionItem();

		upAction.setTitle("Talk");
		upAction.setIcon(getResources().getDrawable(R.drawable.talk));

		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() {
					public void onItemClick(int pos) {

						if (pos == 0) { // Add item selected
							Toast.makeText(MainActivity.this,
									"PHONE item selected", Toast.LENGTH_SHORT)
									.show();
						} else if (pos == 1) { // Accept item selected
							Toast.makeText(MainActivity.this,
									"GMAIL item selected", Toast.LENGTH_SHORT)
									.show();
						} else if (pos == 2) { // Upload item selected
							Toast.makeText(MainActivity.this, "TALK selected",
									Toast.LENGTH_SHORT).show();
						}
					}
				});

		ImageView ivPic1 = (ImageView) this.findViewById(R.id.ivPic1);
		ivPic1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mQuickAction.show(v);
				mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
			}
		});

		Button btClickMe = (Button) this.findViewById(R.id.button1);
		btClickMe.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mQuickAction.show(v);
				mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions