Android Introduction GUI Menu @2011 Mihail L. Sichitiu 1 Many thanks to Jun Bum Lim for his help with this tutorial. Goal Create an application that supports options/sub/context menus Display messages when a menu clicked Automatically fill “Hi!” in the EditText Plus menu will also open a sub-menu <option menu> <sub-menu> @2011 Mihail L. Sichitiu <context menu> 2 Menu Composition Hi Sub1 Plus Hola Sub2 Home Hello <sub-menu> Long press in EditText Pre Next <option menu> <context menu from EditText> @2011 Mihail L. Sichitiu 3 Create HelloMenu Project Create the two TextViews and an EditText Create “menu” folder in res/ Create menu.xml in res/menu/ (New > Other > Android XML File) Create context_menu.xml in res/menu/ @2011 Mihail L. Sichitiu 4 res/menu/menu.xml Define Option menu and sub-menu <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menuItemPlus" android:title="@string/plus" android:icon="@drawable/plus" > Sub-menu items <menu> <item android:id="@+id/menuItemSub1" android:title="@string/sub1"></item> <item android:id="@+id/menuItemSub2" android:title="@string/sub2"></item> </menu> </item> <item android:icon="@drawable/home" android:id="@+id/menuItemHome" android:title="@string/home"></item> <item android:icon="@drawable/pre" android:id="@+id/menuItemPre" android:title="@string/pre"></item> <item android:icon="@drawable/next" android:id="@+id/menuItemNext" android:title="@string/next"></item> </menu> Option menu items @2011 Mihail L. Sichitiu 5 res/menu/context.xml Define context menu for EditText <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <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> @2011 Mihail L. Sichitiu 6 res/values/strings.xml Define constant strings used in the application <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Click Menu Button ! </string> <string name="app_name">Android Menu Example</string> <string <string <string <string name="plus">Plus</string> name="pre">Pre</string> name="next">Next</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> @2011 Mihail L. Sichitiu 7 icons Place icons used in menu.xml in res/drawable/ Download icons at: http://www4.ncsu.edu/~mlsichit/UCAB/resources.html icons @2011 Mihail L. Sichitiu 8 Inflating a option menu resource Inflating a menu resource (menu.xml) by adding onCreateOptionsMenu(Menu menu) in the main Activity. Menu items in menu.xml will appear when the user touches the MENU button public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @2011 Mihail L. Sichitiu 9 Response to user action Response to menu click events by overriding onOptionsItemSelected(Menu menu) in the main Activity. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuItemPlus: Toast.makeText(this, "Plus Button Clicked !", Toast.LENGTH_SHORT).show(); Log.i(TAG,"menuItemPlus"); return true; : : case R.id.menuItemNext: Toast.makeText(this, "Next Button Clicked !", Toast.LENGTH_SHORT).show(); Log.i(TAG,"menuItemNext"); return true; } return false; } @2011 Mihail L. Sichitiu 10 Register View for a context menu By calling registerForContextMenu() and passing it a View (an EditText in this example) you assign it a context menu. When this View (EditText) receives a long-press, it displays a 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); } : : @2011 Mihail L. Sichitiu 11 Define context menu’s appearance By overriding the activity's context menu create callback method, onCreateContextMenu(). @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); } @2011 Mihail L. Sichitiu 12 Define context menu’s behavior By overriding your activity's menu selection callback method for context menu , onContextItemSelected(). @Override public boolean onContextItemSelected(MenuItem item) { Log.i(TAG,"ContextItem selected"); AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.menuItemHi: mOutEditText.setText( this.getResources().getText( R.string.hi_msg) ); return true; case R.id.menuItemHola: : : default: return super.onContextItemSelected(item); } } @2011 Mihail L. Sichitiu 13