Menus and Dialogs Creating Menus By default an

Menus and Dialogs

Creating Menus By default an Activity is associated with a single menu Android creates this single menu and passes it to the on. Create. Options. Menu callback method This method allows you to populate the menu with a set of menu items Once the menu items are populated, the code should return true to make the menu visible If this method returns false, the menu becomes invisible

Creating Menus reate. Options. Menu @Override public boolean on. Create. Options. Menu(Menu menu) { // populate menu items …. . . return true; }

Adding Menu Items Menus Items can grouped together by assigning each one a group ID Multiple menu items that carry the same group ID are considered part of the same group The order of menu items within a menu is defined by the sort -order ID property Some of these order-number ranges are reserved for certain kinds of menus. Secondary menu items: considered less important start at 0 x 30000 and are defined by the constant Menu. CATEGORY_SECONDARY System menus, alternative menus, and container menus—have different order-number ranges

Adding Menu Items @Override public boolean on. Create. Options. Menu(Menu menu) { //call the base class to include system menus super. on. Create. Options. Menu(menu); menu. add(0 // Group id , 1 // item id , 0 // sort order id , ”menu item 1"); // title menu. add(0, 2, 1, ”menu item 2"); menu. add(0, 3, 2, ”menu item 3"); return true; }

Responding to Menu Items Multiple ways of responding to menu-item clicks in Android on. Options. Item. Selected method Listeners Intents

on. Options. Item. Selected When a menu item is clicked, Android calls the on. Options. Item. Selected callback method on the Activity class @Override public boolean on. Options. Item. Selected(Menu. Item item) { switch(item. get. Item. Id()) { //handle menu items click return true; } return super. on. Options. Item. Selected(item); }

Handlin Menu Items with Listeners Menu item allows you to register a listener that could be used as a callback Pass On. Menu. Click. Listener object to the menu item When the menu item is clicked, the menu item will call the on. Menu. Item. Click() method of the On. Menu. Click. Listener object menu. Item. set. On. Menu. Item. Click. Listener(my. Response);

Handlin Menu Items with Listeners The on. Menu. Item. Click method is called when the menu item has been invoked This code executes before the on. Options. Item. Selected method If on. Menu. Item. Click returns true, no other callbacks will be executed

Handlin Menu Items with Intents You can associate a menu item with an intent by using the Menu. Item’s method set. Intent(intent) On click the menu item will invoke the intent using start. Activity(intent) This option will take place only if the menu item was not handled using the on. Options. Item. Selected()

Expanded Menus If an application has more menu items than it can display on the main screen, Android shows the “More” menu item The expanded menu has a limitation: it cannot accommodate icons

Creating Icon Menus Create a regular text-based menu item Use the set. Icon method on the Menu. Item class to set the image Use the image’s resource ID, so you must generate it first by placing the image or icon in the /res/drawable directory Menu. Item menu. Item = menu. add(group, id, order, ”name"); menu. Item. set. Icon(R. drawable. balloons);

Submenus A Menu object can have multiple Sub. Menu objects Each Sub. Menu object is added to the Menu object through a call to the Menu. add. Sub. Menu method You add menu items to a submenu the same way that you add menu items to a menu However, you cannot additional submenus to a submenu Submenus do not support icon menu items

Submenus Sub. Menu sub. Menu = menu. add. Sub. Menu(group, 11, "sub menu"); sub. Menu. add(group, SUB_MENU_1, "sub 1"); sub. Menu. add(group, SUB_MENU_2, "sub 2"); sub. Menu. add(group, SUB_MENU_3, "sub 3"); sub. Menu. add(group, SUB_MENU_4, "sub 4");

Context Menus Contex Menu are “right-click” Menu Android supports context menus through an action called a long click A long click is a mouse click held down slightly longer than usual on any Android view Context Menus are associated with views

Using Context Menus Implementing a context menu: Register a view for a context menu Populate the context menu using Respond to context-menu clicks.

Register a view for a context menu View registration for the context menu is done in on. Create() method of an Activity The registration is done using register. For. Context. Menu method register. For. Context. Menu(this. get. Text. View());

populating a Context Menu Once a view is registered for context menus, Android will call the on. Create. Context. Menu() method with this view as the argument This is where you can populate the context menu items for that context menu (same as regular menu item) @Override public void on. Create. Context. Menu(Context. Menu menu, View v, Context. Menu. Info menu. Info) { … }

Responding to Context Menu Items Android provides a callback method called on. Context. Item. Selected() Use the get. Info method of the received item to get the view to operate on

Responding to Context Menu public boolean on. Context. Item. Selected(Menu. Item item) { Adapter. Context. Menu. Info info = (Adapter. Context. Menu. Info) item. get. Menu. Info(); switch (item. get. Item. Id()) { case EDIT_ID: Text. View tx. View = (Text. View)info. target. View; … return true; case DELETE_ID: … return true; default: return super. on. Context. Item. Selected(item); } }

Creating Menus using XML Define an XML file with menu tags Place the file in the /res/menu subdirectory The name of the file is arbitrary You can have as many files as you want Use the resource ID of the menu file to load the XML file into the menu Respond to the menu items using the resource IDs generated for each menu item

<menu xmlns: android="http: //schemas. android. com/apk/res/android"> <!-- This group uses the default category. --> <group android: id="@+id/menu. Group_Main"> <item android: id="@+id/menu_test. Pick" android: order. In. Category="5" android: title="Test Pick" /> <item android: id="@+id/menu_test. Get. Content" android: order. In. Category="5" android: title="Test Get Content" /> <item android: id="@+id/menu_clear" android: order. In. Category="10" android: title="clear" /> <item android: id="@+id/menu_dial" android: order. In. Category="7" android: title="dial" /> <item android: id="@+id/menu_test" android: order. In. Category="4" android: title="@+string/test" /> </group> </menu>

Inflating XML Menu Android provides a class called android. view. Menu. Inflater to populate Menu objects from XML @Override public boolean on. Create. Options. Menu(Menu menu) { Menu. Inflater inflater = get. Menu. Inflater(); inflater. inflate(R. menu 1, menu); }

Responding to XML-Based Menu private void on. Options. Item. Selected (Menu. Item item) { if (item. get. Item. Id() == R. id. menu_clear) { //do something } … }

Dialogs A dialog is usually a small window that appears in front of the current Activity The underlying Activity loses focus and the dialog accepts all user interaction Dialogs are normally used for notifications and short activities that directly relate to the application in progress.

Dialogs Alert Dialog Progress Dialog Date Picker Dialog Time Picker Dialog

Managing Dialogs A dialog is always created and displayed as a part of an Activity Dialogs are normally created from within the Activity's on. Create. Dialog(int) callback To show a dialog, call show. Dialog(int) and pass it an integer that uniquely identifies the dialog that you want to display

Managing Dialogs When a dialog is requested for the first time, Android calls on. Create. Dialog is where the dialog is instantiated, it is called once for a dialog Before the dialog is displayed, Android also calls the optional callback method on. Prepare. Dialog is used to change any properties of the dialog each time it is opened, it is called each time the dialog is requested

Instantiating Dialogs protected Dialog on. Create. Dialog)int id( { Dialog dialog; switch)id ( { case DIALOG_PAUSED_ID : //do the work to define the pause Dialog break; case DIALOG_GAMEOVER_ID : //do the work to define the game over Dialog break; default : dialog =null; } return dialog }

Alert Dialog An Alert. Dialog is an extension of the Dialog class that contains: A title A text message One, two, or three buttons A list of selectable items (with optional checkboxes or radio buttons)

Building Alert Dialog Alert. Dialog. Builder builder =new Alert. Dialog. Builder)this(; builder. set. Message")Are you sure you want to exit? (". set. Cancelable)false (. set. Positive. Button")Yes", new Dialog. Interface. On. Click. Listener () { public void on. Click)Dialog. Interface dialog, int id ({ My. Activity. this. finish(); } } (. set. Negative. Button")No", new Dialog. Interface. On. Click. Listener (){ public void on. Click)Dialog. Interface dialog, int id ({ dialog. cancel(); } }(; Alert. Dialog alert =builder. create ; ()

Alert Dialog set. Message: adds a message for the dialog set. Cancelable: define if a user can exit from the dialog using the back button set. Positive. Button: adds buttons to the dialog

Progress Dialog A Progress. Dialog is an extension of the Alert. Dialog class that can display a progress animation in the form of a spinning wheel The dialog can also provide buttons, such as one to cancel a download Progress. Dialog dialog =Progress. Dialog. show) My. Activity. this, "", "Loading. Please wait". . . , true);

Time. Picker Dialog public Time. Picker. Dialog ( Context context, Time. Picker. Dialog. On. Time. Set. Listener callback, Int hour. Of. Day, int minute, boolean is 24 Hour. View) call. Back. How parent is notified. hour. Of. Day. The initial hour. minute. The initial minute. is 24 Hour. View. Whether this is a 24 hour view, or AM/PM.

Date. Picker Dialog Public Date. Picker. Dialog ( Context context, Date. Picker. Dialog. On. Date. Set. Listener callback, Int year, int month. Of. Year, int day. Of. Month) context. The context the dialog is to run in. call. Back. How the parent is notified that the date is set. year. The initial year of the dialog. month. Of. Year. The initial month of the dialog. day. Of. Month. The initial day of the dialog.

Custom Dialog Context m. Context =get. Application. Context(); Dialog dialog =new Dialog)m. Context(; dialog. set. Content. View)R. layout. custom_dialog(; dialog. set. Title")Custom Dialog("; Text. View text ) =Text. View ( dialog. find. View. By. Id)R. id. text(; text. set. Text")Hello, this is a custom dialog("!; Image. View image ) =Image. View ( dialog. find. View. By. Id)R. id. image(; image. set. Image. Resource)R. drawable. android ; (

Custom Dialog <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: id="@+id/layout_root" android: orientation="horizontal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: padding="10 dp" > <Image. View android: id="@+id/image" android: layout_width="wrap_content" android: layout_height="fill_parent" android: layout_margin. Right="10 dp" /> <Text. View android: id="@+id/text" android: layout_width="wrap_content" android: layout_height="fill_parent" android: text. Color="#FFF" /> </Linear. Layout>
- Slides: 37