Using Context Menus in Android

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 item clicks.

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 item 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 FaYnaSoft 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.

  • Share/Bookmark

Related posts:

  1. Using Submenu in Android
  2. Handling Options Menu item selections in Android
  3. Loading Menus through XML files in Android
  4. Operations with Options Menu items group in Android
  5. Getting formatted string from strings.xml in Android

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>