1 / 13

Android Menu Example

Learn how to create a menu in an Android application that supports options/sub/context menus and displays messages when a menu is clicked.

bprince
Download Presentation

Android Menu Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android Introduction GUI Menu @2011 Mihail L. Sichitiu Many thanks to Jun Bum Lim for his help with this tutorial.

  2. Goal • Create an application that supports options/sub/contextmenus Display messages whena menu clicked Automatically fill “Hi!”in the EditText Plus menu willalso open a sub-menu <option menu> <sub-menu> <context menu> @2011 Mihail L. Sichitiu

  3. Menu Composition Hi Sub1 Plus Hola Sub2 Hello Long press in EditText Home <sub-menu> Pre Next <context menu from EditText> <option menu> @2011 Mihail L. Sichitiu

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

  5. 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" > <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> Sub-menu items Option menu items @2011 Mihail L. Sichitiu

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

  7. 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 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> @2011 Mihail L. Sichitiu

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

  9. 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 booleanonCreateOptionsMenu(Menu menu) { MenuInflaterinflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @2011 Mihail L. Sichitiu

  10. Response to user action • Response to menu click events by overriding onOptionsItemSelected(Menu menu) in the main Activity. @Override public booleanonOptionsItemSelected(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

  11. 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 EditTextmOutEditText; @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

  12. Define context menu’s appearance • By overriding the activity's context menu create callback method, onCreateContextMenu(). @Override public voidonCreateContextMenu(ContextMenu menu, View v, ContextMenuInfomenuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflaterinflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); } @2011 Mihail L. Sichitiu

  13. Define context menu’s behavior • By overriding your activity's menu selection callback method for context menu , onContextItemSelected(). @Override public booleanonContextItemSelected(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

More Related