Home > Android > Context menu

Context menu

Define context menu EditText: /res/menu/context.xml

 <?xml version=”1.0″ encoding=”utf-8″?>



<item android:id=”@+id/menuItemHi” android:title=”@string/hi_msg”></item>

<item android:id=”@+id/menuItemHola” android:title=”@string/hola_msg”></item>

<item android:id=”@+id/menuItemHello” android:title=”@string/hello_msg”></item>


Define constant strings used in application: /res/values/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>


<string name=”hello”>Click Menu Button ! </string>

<string name=”app_name”>Android Menu Example</string>

<string name=”plus”>Plus</string>

<string name=”pre”>Pre</string>

<string name=”next”>Next</string>

<string name=”home”>Home</string>

<string name=”sub1″>Sub1</string>

<string name=”sub2″>Sub2</string>

<string name=”hi_msg”>Hi !</string>

<string name=”hola_msg”>Hola !</string>

<string name=”hello_msg”>Hello !</string>


Register VIEW for context menu:

public class AndroidMenuExampleActivity extends Activity {

private EditText mOutEditText;


public void onCreate(Bundle savedInstanceState) {



mOutEditText = (EditText) findViewById(R.id.editText);



Define context menu’s appereance:


public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenuInfo menuInfo) {

super.onCreateContextMenu(menu, v, menuInfo);

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.context_menu, menu);


Define context menu’s behavior:


public boolean onContextItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.menuItemHi:

//Task 1


case R.id.menuItemHola:

//Task 2



return super.onContextItemSelected(item);


Source:  Internet

Categories: Android
  1. No comments yet.
  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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: