Home > Android, Android how to, Menu > Using Context Menus in Android

Using Context Menus in Android


We moved. Please visit this link for this post.

 

 

 

Context menu in Android is similar to context menu in any others OS.

In Microsoft Windows or in any Linux distr you click right mouse key and see context menu. The same is in Android, but you press and hold your view which has context menu.

I am going to show how to create, register and handle context menu.

First off all need to register view for using context menu:

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

Next need to create context menu. For this need to override this method:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	super.onCreateContextMenu(menu, v, menuInfo);
	menu.setHeaderTitle("My Context Menu");
	menu.add(0, NEW_MENU_ITEM, 0, "new");
	menu.add(0, SAVE_MENU_ITEM, 1, "save");
}

And last one need to handle menu clicks:

@Override
public boolean onContextItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case NEW_MENU_ITEM:
		doSomething();
		break;
	case SAVE_MENU_ITEM:
		doSomething();
		break;
	}
	return super.onContextItemSelected(item);
}

And as usual full example:

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Class which shows how to use context menus
 *
 * @author FaYna Soft Labs
 */
public class Main extends Activity {
	private static final int NEW_MENU_ITEM = Menu.FIRST;
	private static final int SAVE_MENU_ITEM = NEW_MENU_ITEM + 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView tv = (TextView) findViewById(R.id.textId);
		tv.setText("TextView element");
		registerForContextMenu(tv);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case NEW_MENU_ITEM:
			showMsg("New");
			break;
		case SAVE_MENU_ITEM:
			showMsg("Save");
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("My Context Menu");
		menu.add(0, NEW_MENU_ITEM, 0, "New");
		menu.add(0, SAVE_MENU_ITEM, 1, "Save");
	}

	private void showMsg(String message) {
		Toast msg = Toast.makeText(Main.this, message, Toast.LENGTH_LONG);
		msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
				msg.getYOffset() / 2);
		msg.show();
	}
}

How to test? It is very easy just press and hold TextView element.

As you can see Context Menu is similar to other menus in Android.

About these ads
Categories: Android, Android how to, Menu
  1. Shrinidhi
    December 8, 2009 at 10:04 am

    Broken code. What does variable R signify in onCreate?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: