Download - Android Using Menus
![Page 1: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/1.jpg)
Politeknik Elektronika Negeri Surabaya 1
AndroidUsing Menus
Yuliana SetiowatiRizky Yuniar HakkunAhmad Syauqi Ahsan
![Page 2: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/2.jpg)
Politeknik Elektronika Negeri Surabaya 2
Outline
• ContextMenu• OptionMenu
![Page 3: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/3.jpg)
Politeknik Elektronika Negeri Surabaya 3
Using Menus
Menus is not consuming ‘much’ view space. Android provides two types of menu known as: options menu and context menu.– The options menu is triggered by pressing the
hardware Menu button on the device, while– The context menu is raised by a tap-and-hold on
the widget associated to the menu.
![Page 4: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/4.jpg)
Politeknik Elektronika Negeri Surabaya 4
Using MenusExample: Using an option menu
Options available in this context
Press Menu Button
A max of six entries per menu. Excess will be displayed as part of the More option
![Page 5: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/5.jpg)
Politeknik Elektronika Negeri Surabaya 5
Using Menus
Example: Using an option menu
Available Options in this context
Press Menu button
![Page 6: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/6.jpg)
Politeknik Elektronika Negeri Surabaya 6
Using Menus
Example :Dealing with SMS (text-messages) by using the built-in Messaging app’scontext menu
Tap-&-Hold
![Page 7: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/7.jpg)
Politeknik Elektronika Negeri Surabaya 7
Using MenusComments on Creating Context Menu
• Indicate which widget(s) on your activity have context menus. To do this, call registerForContextMenu(theWidget)
• supplying the View that is the widget needing a context menu.Implement onCreateContextMenu(…)
• To find out when a context menu choice was chosen, implement onContextItemSelected() on the activity.
![Page 8: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/8.jpg)
Politeknik Elektronika Negeri Surabaya 8
Using Menus
Comments on Creating Option Menu
• Pressing the hardware Menu button on the device.Implement onCreateOptionMenu(…)
• To find out when a option menu choice was chosen, implement onOptionItemSelected() on the activity.
![Page 9: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/9.jpg)
Politeknik Elektronika Negeri Surabaya 9
Using MenusExample 1
Example 1: Using an Option Menu
In this application the Option menu offers a way of changing the text size (on both EditText boxes)
Click on Menubutton to see size options
After choosing option:50 points
![Page 10: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/10.jpg)
Politeknik Elektronika Negeri Surabaya 10
Using MenusExample 1
• Example 1: Using an Option Menu
Up to six options will be displayed on the Option menu.If there are more than six options pushing the More button will show the rest
Click the More button to see additional options
Additional choices of the Option menu
![Page 11: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/11.jpg)
Politeknik Elektronika Negeri Surabaya 11
Using MenusExample 1
• Example 1: Using a Context Menu
Each view could have an associated Context Menu
Long-press a textbox to invoke its Context Menu
![Page 12: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/12.jpg)
Politeknik Elektronika Negeri Surabaya 12
Using MenusExample 1
• Example 1: Using Option and Context MenuThe app shows two text boxes. Menus are used to change text’s size, color, and style.
![Page 13: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/13.jpg)
Politeknik Elektronika Negeri Surabaya 13
Using MenusExample 1
• Example 1: Using Option and Context MenuThe app shows two text boxes. Menus are used to change text’s size, color, and style.
![Page 14: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/14.jpg)
Politeknik Elektronika Negeri Surabaya 14
Using MenusExample 1
• Example 1: Using Option and Context MenuThe app shows two text boxes. Menus are used to change text’s size, color, and style.
![Page 15: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/15.jpg)
Politeknik Elektronika Negeri Surabaya 15
Using MenusExample 1
• Example 1: Using Option and Context MenuThe app shows two text boxes. Menus are used to change text’s size, color, and style.
![Page 16: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/16.jpg)
Politeknik Elektronika Negeri Surabaya 16
Using MenusExample 1
• Example 1: Using Option and Context MenuThe app shows two text boxes. Menus are used to change text’s size, color, and style.
![Page 17: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/17.jpg)
Politeknik Elektronika Negeri Surabaya 17
Using MenusExample 1
• Example 1: Using Option and Context MenuThe app shows two text boxes. Menus are used to change text’s size, color, and style.
![Page 18: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/18.jpg)
Politeknik Elektronika Negeri Surabaya 18
Using MenusExample 1
• Example 1: Using Option and Context MenuThe app shows two text boxes. Menus are used to change text’s size, color, and style.
![Page 19: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/19.jpg)
Politeknik Elektronika Negeri Surabaya 19
Using MenuExample 2
• Example 2: Enhancing Option/Context MenuThis extension of the previous example adds icons and links to the Option Menu entries, as well as a sub-menu list tied to one the option menu buttons.
Click on SubMenu to see additional options
![Page 20: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/20.jpg)
Politeknik Elektronika Negeri Surabaya 20
Using MenuExample 2
• Example 2: Enhancing Option/Context MenuThis extension of the previous example adds icons and linksto the Option Menu entries, as well as a sub-menu list tied to one the option menu buttons.
![Page 21: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/21.jpg)
Politeknik Elektronika Negeri Surabaya 21
Using MenuExample 2
• Example 2: Enhancing Option/Context Menu
Replace the method populateMyFirstMenuwith the following code
![Page 22: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/22.jpg)
Politeknik Elektronika Negeri Surabaya 22
Using MenuExample 2
• Example 2: Enhancing Option/Context Menu
Continuation…Replace the method populateMyFirstMenuwith the following code
![Page 23: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/23.jpg)
Politeknik Elektronika Negeri Surabaya 23
Using MenuExample 2
• Example 2: Enhancing Option/Context Menu
Continuation…Replace the method applyMenuOptionwith the following code
![Page 24: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/24.jpg)
Politeknik Elektronika Negeri Surabaya 24
Using MenuExample 2
• Example 2: Enhancing Option/Context Menu
Continuation…Replace the method applyMenuOptionwith the following codeSame as before
![Page 25: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/25.jpg)
Politeknik Elektronika Negeri Surabaya 25
Using Menu
Comments on Creating Submenus• Submenus are displayed as regular Menus Items.
When selected they just display more options. • To avoid cluttered UIs, submenus are not displayed
using three-like structure, instead they appear as a simple floating window presenting a list of items.
• Android does not support nested submenus.
![Page 26: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/26.jpg)
Politeknik Elektronika Negeri Surabaya 26
Using Menu• Comments on Creating Submenus
Example: Extend previous example by adding a fifth entry to the existing menu list. The new entry is of the SubMenutype. It should offer three options of its own.
Note: add this code to the end of the: onCreateOptionsMenu(…)method.
![Page 27: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/27.jpg)
Politeknik Elektronika Negeri Surabaya 27
Using MenuExample 3
• Example 3: Using Option/Context Menu
Observe how the selection made from the Menu changes the inter-row spacing of the ListView.
Click on Menu to see spacing options
After choosing option:24 Pixelsof row separation
![Page 28: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/28.jpg)
Politeknik Elektronika Negeri Surabaya 28
Using MenuExample 3
• Example 3: Using Option
After choosing option:Morea new menu is shown
Click on Menu to see spacing options
![Page 29: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/29.jpg)
Politeknik Elektronika Negeri Surabaya 29
Using MenuExample 3
• Example 3: Using a Context Menu
Choices listed in a floating ListView
Tap & Holdany row to bringContext Menu
![Page 30: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/30.jpg)
Politeknik Elektronika Negeri Surabaya 30
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 31: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/31.jpg)
Politeknik Elektronika Negeri Surabaya 31
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 32: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/32.jpg)
Politeknik Elektronika Negeri Surabaya 32
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 33: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/33.jpg)
Politeknik Elektronika Negeri Surabaya 33
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 34: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/34.jpg)
Politeknik Elektronika Negeri Surabaya 34
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 35: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/35.jpg)
Politeknik Elektronika Negeri Surabaya 35
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 36: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/36.jpg)
Politeknik Elektronika Negeri Surabaya 36
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 37: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/37.jpg)
Politeknik Elektronika Negeri Surabaya 37
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 38: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/38.jpg)
Politeknik Elektronika Negeri Surabaya 38
Using MenuExample 3
• Example 3: Creating an Option & Context Menu
![Page 39: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/39.jpg)
Politeknik Elektronika Negeri Surabaya 39
Using MenuExample 4
• Example 4: Another App Using Menus that include Icons & Text
This is a simple application designed around a TableLayout. 1. The first row shows a summary of the data collected from the screen. 2. The second row exposes a RadioGroupand a textBox. 3. The third row shows a list of the calls made to the option and context menus.
This Option Menu shows its options using text & icons
![Page 40: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/40.jpg)
Politeknik Elektronika Negeri Surabaya 40
Using MenuExample 4
• Example 4: Another App Using Menus that include Icons & Text
Application’s Layout
![Page 41: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/41.jpg)
Politeknik Elektronika Negeri Surabaya 41
Using MenuExample 4
• Example 4: Another App Using Menus that include Icons & Text
Application’s Layout.Hierarchy View
![Page 42: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/42.jpg)
Politeknik Elektronika Negeri Surabaya 42
Using MenuExample 4
![Page 43: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/43.jpg)
Politeknik Elektronika Negeri Surabaya 43
Using MenuExample 4
![Page 44: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/44.jpg)
Politeknik Elektronika Negeri Surabaya 44
Using MenuExample 4
![Page 45: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/45.jpg)
Politeknik Elektronika Negeri Surabaya 45
Using MenuExample 4
![Page 46: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/46.jpg)
Politeknik Elektronika Negeri Surabaya 46
Using MenuExample 4
• Example 4. Menu Using Icons & TextThis custom layout (main1.xml) is used to show the ListView rows (shown as red rows –big font )
![Page 47: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/47.jpg)
Politeknik Elektronika Negeri Surabaya 47
Using MenuExample 4
• Example 4. Menu Using Icons & Text
![Page 48: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/48.jpg)
Politeknik Elektronika Negeri Surabaya 48
Using MenuExample 4
• Example 4. Menu Using Icons & Text
![Page 49: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/49.jpg)
Politeknik Elektronika Negeri Surabaya 49
Using MenuExample 4
• Example 4. Menu Using Icons & Text
![Page 50: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/50.jpg)
Politeknik Elektronika Negeri Surabaya 50
Using MenuExample 4
• Example 4. Menu Using Icons & Text
![Page 51: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/51.jpg)
Politeknik Elektronika Negeri Surabaya 51
Using MenuExample 4
• Example 4. Menu Using Icons & Text
![Page 52: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/52.jpg)
Politeknik Elektronika Negeri Surabaya 52
Using MenuExample 4
• Example 4. Menu Using Icons & Text
![Page 53: Android Using Menus](https://reader036.vdocuments.site/reader036/viewer/2022062323/568160d8550346895dd009c2/html5/thumbnails/53.jpg)
Politeknik Elektronika Negeri Surabaya 53
Using Menu• Comments on Creating Submenus
Example: Extend previous example by adding a fifth entry to the existing menu list. The new entry is of the SubMenutype. It should offer three options of its own.