Home > Android > Context menu

Context menu


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

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

<menu

xmlns:android=”http://schemas.android.com/apk/res/android”&gt;

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

</menu>

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

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

<resources>

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

</resources>

Register VIEW for context menu:

public class AndroidMenuExampleActivity extends Activity {

private EditText mOutEditText;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

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

registerForContextMenu(mOutEditText);

}

Define context menu’s appereance:

@Override

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:

@Override

public boolean onContextItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.menuItemHi:

//Task 1

break;

case R.id.menuItemHola:

//Task 2

break;

}

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 )

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

%d bloggers like this: