Android Introduction GUI Menu 1 Goal p Create













- Slides: 13

Android Introduction GUI Menu 1

Goal p Create an application that supports options/sub/context menus Display messages when a menu clicked Automatically fill “Hi!” in the Edit. Text Plus menu will also open a sub-menu <option menu> <sub-menu> <context menu> 2

Menu Composition Sub 1 Plus Sub 2 Home <sub-menu> Hi Hola Hello Long press in Edit. Text Pre Next <option menu> <context menu from Edit. Text> 3

Create Hello. Menu Project p p Create the two Text. Views and an Edit. Text Create “menu” folder in res/ Create menu. xml in res/menu/ (New > Other > Android XML File) Create context_menu. xml in res/menu/ 4

res/menu. xml p 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/menu. Item. Plus" android: title="@string/plus" android: icon="@drawable/plus" > Sub-menu items <menu> <item android: id="@+id/menu. Item. Sub 1" android: title="@string/sub 1"></item> <item android: id="@+id/menu. Item. Sub 2" android: title="@string/sub 2"></item> </menu> </item> <item android: icon="@drawable/home" android: id="@+id/menu. Item. Home" android: title="@string/home"></item> <item android: icon="@drawable/pre" android: id="@+id/menu. Item. Pre" android: title="@string/pre"></item> <item android: icon="@drawable/next" android: id="@+id/menu. Item. Next" android: title="@string/next"></item> </menu> Option menu items 5

res/menu/context. xml p Define context menu for Edit. Text <? xml version="1. 0" encoding="utf-8"? > <menu xmlns: android="http: //schemas. android. com/apk/res/android"> <item android: id="@+id/menu. Item. Hi" android: title="@string/hi_msg"></item> <item android: id="@+id/menu. Item. Hola" android: title="@string/hola_msg"></item> <item android: id="@+id/menu. Item. Hello" android: title="@string/hello_msg"></item> </menu> 6

res/values/strings. xml p 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> name="pre">Pre</string> name="next">Next</string> name="home">Home</string> <string name="sub 1">Sub 1</string> <string name="sub 2">Sub 2</string> <string name="hi_msg">Hi !</string> <string name="hola_msg">Hola !</string> <string name="hello_msg">Hello !</string> </resources> 7

icons p Place icons used in menu. xml in res/drawable/ p Download icons at: icons 8

Inflating a option menu resource p p Inflating a menu resource (menu. xml) by adding on. Create. Options. Menu(Menu menu) in the main Activity. Menu items in menu. xml will appear when the user touches the MENU button public boolean on. Create. Options. Menu(Menu menu) { Menu. Inflater inflater = get. Menu. Inflater(); inflater. inflate(R. menu, menu); return true; } 9

Response to user action p Response to menu click events by overriding on. Options. Item. Selected(Menu menu) in the main Activity. @Override public boolean on. Options. Item. Selected(Menu. Item item) { switch (item. get. Item. Id()) { case R. id. menu. Item. Plus: Toast. make. Text(this, "Plus Button Clicked !", Toast. LENGTH_SHORT). show(); Log. i(TAG, "menu. Item. Plus"); return true; : : case R. id. menu. Item. Next: Toast. make. Text(this, "Next Button Clicked !", Toast. LENGTH_SHORT). show(); Log. i(TAG, "menu. Item. Next"); return true; } return false; } 10

Register View for a context menu p p By calling register. For. Context. Menu() and passing it a View (an Edit. Text in this example) you assign it a context menu. When this View (Edit. Text) receives a long-press, it displays a context menu. public class Android. Menu. Example. Activity extends Activity { private Edit. Text m. Out. Edit. Text; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); m. Out. Edit. Text = (Edit. Text) find. View. By. Id(R. id. edit. Text); register. For. Context. Menu(m. Out. Edit. Text); } : : 11

Define context menu’s appearance p By overriding the activity's context menu create callback method, on. Create. Context. Menu(). @Override public void on. Create. Context. Menu(Context. Menu menu, View v, Context. Menu. Info menu. Info) { super. on. Create. Context. Menu(menu, v, menu. Info); Menu. Inflater inflater = get. Menu. Inflater(); inflater. inflate(R. menu. context_menu, menu); } 12

Define context menu’s behavior p By overriding your activity's menu selection callback method for context menu , on. Context. Item. Selected(). @Override public boolean on. Context. Item. Selected(Menu. Item item) { Log. i(TAG, "Context. Item selected"); Adapter. Context. Menu. Info info = (Adapter. Context. Menu. Info) item. get. Menu. Info(); switch (item. get. Item. Id()) { case R. id. menu. Item. Hi: m. Out. Edit. Text. set. Text( this. get. Resources(). get. Text( R. string. hi_msg) ); return true; case R. id. menu. Item. Hola: : : default: return super. on. Context. Item. Selected(item); } } 13