Date post: | 03-Jun-2018 |
Category: |
Documents |
Upload: | heber-medina |
View: | 230 times |
Download: | 0 times |
of 26
8/12/2019 3-Android Ui Menu
1/26
1
Android UI - MenusAndroid UI - Menus
Sang ShinSang ShinMichle GarocheMichle Garoche
www.javapassion.comwww.javapassion.comLearn with Passion!Learn with Passion!
1
8/12/2019 3-Android Ui Menu
2/26
2
Disclaimer
Portions of this presentation are modificationsbased on work created and shared by theAndroid Open Source Project>
http://code.google.com/policies.html They are used according to terms described in
the reati!e ommons ".# Attribution $icense> http://creati!ecommons.org/licenses/by/".#/
8/12/2019 3-Android Ui Menu
3/26
3
Topics
Types of menus
Options menu
onte%t menu
Submenu
reating menu using &enu resource
8/12/2019 3-Android Ui Menu
4/26
Types of MenusTypes of Menus
8/12/2019 3-Android Ui Menu
5/26
8/12/2019 3-Android Ui Menu
6/26
Context MenuContext Menu
8/12/2019 3-Android Ui Menu
7/267
Context Menu
onte%t menus donotsupport itemshortcuts and itemicons.
8/12/2019 3-Android Ui Menu
8/268
How to Create Context Menu?
hen onte%t menu is opened for the firsttime0 the Android system will call the Acti!ity1sonCreateContextMenu(Menu menu)callbackmethod.> 2ou0 as a conte%t menu de!eloper0 o!erride this
method in your Acti!ity class and populate the &enuobject gi!en to you with MenuItem's.
2ou can populate the menu in two ways> Scheme 34: by calling add56 for each item you1d like
in the menu.> Scheme 3": by inflating a menu resource that was
defined in 7&$ 5preferred6
8/12/2019 3-Android Ui Menu
9/269
Populating Menu wit Menu Items! "#
// O!erride this method of Acti!ity class in order to create menu items. 8O!erride public !oid onreateonte%t&enu5 onte%t&enu menu0 // onte%t menu that is being built )iew !iew0 // The !iew for which the conte%t menu is being built
onte%t&enu9nfo menu9nfo6
super.onreateonte%t&enu5menu0 !iew0 menu9nfo6; menu.set0 &enu.'9*ST 0 &enu.,O,+0 =menu 34=6; menu.add5>0 &enu.'9*ST ? 40 &enu.,O,+0 =menu 3"=6;
menu.add5>0 &enu.'9*ST ? "0 &enu.,O,+0 =menu 3@=6; menu.add5>0 &enu.'9*ST ? @0 &enu.,O,+0 =menu 3=6; B
8/12/2019 3-Android Ui Menu
10/2610
How to andle User$s Menu %election?
hen a menu item is selected by a user fromthe onte%t &enu0 onContextItemSelected()callback method of your Acti!ity gets called> This callback passes you the MenuItemthat has been
selected.> 2ou can identify the item by reCuesting the item9d0
with getItemId()0 which returns the integer that wasassigned with the add5int group9d0 int item9d0 intorder0 harSeCuence title6method.
> Once you identify the menu item0 you can take anappropriate action.
8/12/2019 3-Android Ui Menu
11/2611
&xample! Handling Menu %election
/D
8/12/2019 3-Android Ui Menu
12/26
'ptions Menu'ptions Menu
8/12/2019 3-Android Ui Menu
13/2613
(en to use 'ptions Menu?
The Options &enu iswhere you shouldinclude basicapplication functionsand any necessaryna!igation items5e.g.0 to a home
screen or applicationsettings6.
8/12/2019 3-Android Ui Menu
14/2614
How 'ptions Menu (or)?
The Options &enu is opened by pressing thede!ice &+,- key.
hen opened0 the 9con &enu is displayed0
which holds the first si% menu items. 9f more than si% items are added to the Options
&enu0 then those that can1t fit in the 9con &enuare re!ealed in the +%panded &enu0 !ia the
=&ore= menu item.
8/12/2019 3-Android Ui Menu
15/2615
Populating Menu wit Menu Items! "#
/D reates the menu items without 9consD/public boolean onreateOptions&enu5&enu menu6 // The add56 method used in this sample takes four arguments:
// group9d0 item9d0 order0 and title. menu.add5>0 &+,-E,+EFA&+0 >0 =,ew Fame=6; menu.add5>0 &+,-EG-9T0 >0 =Guit=6; return true;B
/D reates the menu items with 9cons. ,ote that add56 method returns newly created &enu9tem object to set additional properties like an icon0
a keyboard shortcut0 an intent0 and other settings for the item. D/public boolean onreateOptions&enu5&enu menu6
// The add56 method used in this sample takes four arguments:// group9d0 item9d0 order0 and title. menu.add5>0 &+,-E,+EFA&+0 >0
=,ew Fame=6.set9con5*.drawable.menuEnewEgameEicon6; menu.add5>0 &+,-EG-9T0 >0 =Guit=6.set9con5*.drawable.menuECuitEicon6; return true;B
8/12/2019 3-Android Ui Menu
16/2616
How to andle Menu %election?
hen a menu item is selected from the Options&enu0 onOptionsItemSelected()callbackmethod of your Acti!ity gets called> This callback passes you the MenuItemthat has been
selected.> 2ou can identify the item by reCuesting the item9d0
with getItemId()0 which returns the integer that wasassigned with the add56 method.
>
Once you identify the menu item0 you can take anappropriate action.
8/12/2019 3-Android Ui Menu
17/2617
&xample! Handling Menu %election
/D
8/12/2019 3-Android Ui Menu
18/26
%u*menu%u*menu
8/12/2019 3-Android Ui Menu
19/2619
(en to use %u*Menu?
9f you ha!e se!eral menu items that can begrouped together with a title0 considerorganiing them into a Submenu.
8/12/2019 3-Android Ui Menu
20/2620
&xample! Creating %u*menu
public boolean onreateOptions&enu5&enu menu6 boolean result H super.onreateOptions&enu5menu6;
// reate submenu ='ile= Sub&enu file&enu H menu.addSub&enu5='ile=6;
file&enu.add5=,ew=6; file&enu.add5=Open 'ile=6; file&enu.add5=lose=6; file&enu.add5=lose All=6;
// reate submenu =+dit=
Sub&enu edit&enu H menu.addSub&enu5=+dit=6; edit&enu.add5=-ndo Typing=6; edit&enu.add5=*edo=6; edit&enu.add5=ut=6;
return result;
B
8/12/2019 3-Android Ui Menu
21/26
Creating Menu usingCreating Menu usingMenu +esourceMenu +esource
8/12/2019 3-Android Ui Menu
22/26
22
(at is , (y using Menu +esource?
9nstead of instantiating &enu objects in yourapplication code0 you should define a menu andall its items in an 7&$ menu resource0 theninflate the menu resource 5load it as aprogrammable object6 in your application code.
Iefining your menus in 7&$ is a better practice5than creating them in code6 because it
separates your interface design from yourapplication code 5the same as when you defineyour Acti!ity layout in 7&$6.
8/12/2019 3-Android Ui Menu
23/26
23
(en to Use Menu +esource ile?
reate JmenuEresourceK.%ml under res/menu/directory
9nflate the &enu *esource file using
inflate()method of theMenuInflatorclass> &enu objects are created from the &enu resource
file
8/12/2019 3-Android Ui Menu
24/26
24
&xample! Inflating Menu +esource
public boolean onreateOptions&enu5&enu menu6 // 9nflate the menu 7&$ resource. &enu9nflater inflater H get&enu9nflater56; inflater.inflate5*.menu.titleEonly0 menu6;
return true; B
8O!erride public boolean onOptions9temSelected5&enu9tem item6
switch 5item.get9tem9d566 case *.id.jump: Toast.makeTe%t5this0 =Lump up in the airM=0 Toast.$+,FT
8/12/2019 3-Android Ui Menu
25/26
25
&xample! Menu +esource ile
Jmenu %mlns:androidH=http://schemas.android.com/apk/res/android=K
Jitem android:idH=8?id/jump= android:titleH=LumpM= android:iconH=8drawable/drawEjump= /K
Jitem android:idH=8?id/di!e= android:titleH=Ii!eM= android:iconH=8drawable/drawEdi!e= /K
J/menuK
8/12/2019 3-Android Ui Menu
26/26
Tan) you.Tan) you.
Check JavaPassion.com Coecamps!Check JavaPassion.com Coecamps!http""www.javapassion.com"coecampshttp""www.javapassion.com"coecamps
Learn with Passion!Learn with Passion!
http://www.javapassion.com/codecampshttp://www.javapassion.com/codecampshttp://www.javapassion.com/codecamps