Your SlideShare is downloading. ×
0
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Mobile Application Development: Lecture 07
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Mobile Application Development: Lecture 07

738

Published on

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
738
On Slideshare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Mobile Application Development Lecture 07 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Menus ● Key Menu Classes ● Menu Construction ● Grouping Menu Items ● Responding to Menu Item Selectionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 3. Key Menu Classes ● The key classes: – android.view.Menu – android.view.SubMenu – android.view.MenuItem ● Every Activity contains a single Menu ● A Menu consists 0 or more MenuItems ● A Menu contains 0 or more SubMenus ● A SubMenu contains 0 or more MenuItemshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 4. Menu Constructionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 5. Options Menu ● Activity is associated with a single Menu ● Such menus in are called options menus ● Android creates an option menu automatically once via the onCreateOptionsMenu() callback method ● This is the method where developers can populate Menu with MenuItemshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 6. onCreateOptionsMenu() Callback public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater() .inflate(R.menu.activity_simple_menu, menu); return true } ● If the method returns true, the Menu is made visible ● If the method returns false, the Menu is made invisiblehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 7. onCreateOptionsMenu() Callback ● This callback is called only once when the options menu is created for the first time ● If the options menu must be updated/changed every time it is displayed, onPrepareOptionsMenu(Menu) ● Once the menu is created, the callback method onOptionsItemSelected(MenuItem) can be implemented to handle MenuItem choiceshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 8. onOptionsMenuSelected() Callback ● This callback is called only a MenuItem is selected ● The default implementation returns false, in which case the items Runnable will be called or a message is sent to the items Handler ● If the item selection is consumed within the callback it must return truehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 9. SimpleMenuApphttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 10. Gus Khrustalny (Chrystal Goose), Russia Source: http://images.yandex.ruhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 11. Image Browsing App with a Menu ● Develop an application that uses an simple menu and an ImageView to browse linearly (forward and backward) through a small set of images from Gus Khrustalnyi, Russia ● The menu should have three items: info, next, and previous ● When info is selected, information associated with the current image should be toasted ● When next/previous is selected, the ImageView should display next/previous imagehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 12. Main XML Layout <LinearLayout android:id="@+id/LinearLayout1" android:orientation="vertical" > <TextView android:id="@+id/tv_info" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img_view_01" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 13. XML Menu Layout <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/mi_info" android:icon="@android:drawable/ic_menu_info_details"> </item> <item android:id="@+id/mi_next" android:checkable="true" android:title="@string/mi_next_title"> </item> <item android:id="@+id/mi_backward" android:title="@string/mi_previous_title"> </item> </menu>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 14. onOptionsItemSelected() Callback public boolean onOptionsItemSelected(MenuItem item) { switch ( item.getItemId() ) { case R.id.mi_backward: setPreviousImage(); setInfoMessage(); break; case R.id.mi_next: setNextImage(); setInfoMessage(); break; case R.id.mi_info: displayInfoMessage(); break; } return true; }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 15. ImageView Manipulation switch ( mCurImgNum ) { // other cases case 1: mImgView.setBackgroundResource(R.drawable.img_01); break; // other cases }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 16. Grouping Menu Itemshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 17. Menu Item Groups ● Menu items can be grouped by assigning each item a group ID ● Multiple menu items that have the same group ID are considered part of the same group ● Android API provides a set of methods that take group IDs and enable a specific functionality for all menu items in a selected grouphttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 18. Grouping Menu Items public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); // 0 – group ID, 1 – item id, 2 – sort-order ID, string is title // “item 1” - title; Menu.NONE can be used for group, item, // and sort-order menu.add(0, 1, 2, “item 1”); menu.add(0, 2, 3, “item 2”); menu.add(0, 3, 4, “item 3”); return true }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 19. Menu Group Construction public boolean onCreateOptionsMenu(Menu menu) { int gid1 = 1; menu.add(gid1, 1, 1, “group 1 item 1”); menu.add(gid1, 2, 2, “group 1 item 2”); int gid2 = 2; menu.add(gid2, 3, 3, “group 2 item 1”); menu.add(gid2, 4, 4, “group 2 item 2”); return true }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 20. Menu Group Manipulation ● removeGroup(gid) – removes all menu items from that group ● setGroupCheckable(gid, checkable, exclusive) – show a check mark for all menu items in the group; if exclusive flag is true, only one menu item can be checked ● setGroupEnabled(gid, enabled) ● setGroupVisible(gid, visible)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 21. Responding to Menu Item Selectionshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 22. Three Response Options ● onOptionsItemSelected – override this method for the Activity class ● onMenuClickListener – create your own click listener and implement onMenuItemClick ● Intent - use setIntent method of MenuItem; intents are called with setIntent(i)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 23. Response 1: onOptionsItemSelected @Override public boolean onOptionsItemSelected(MenuItem mi) { switch ( mi.getItemId() ) { ... } return true; } ● For the menu items that are handled, this method should return truehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 24. Response 2: onOptionsItemSelected public class MyResponse implements OnMenuClickListener { public boolean onMenuItemClick(MenuItem item) { …. } } MyResponse myRes = new MyResponse(...) menu.setOnItemClickListener(myRes)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 25. Response 3: Intent ● Override onOptionsItemSelected() and invoke the parent for the menu items that are not handled ● If you do not invoke the parent, the Intents may not be handled properlyhttp://www.youtube.com/vkedco
  • 26. References ● http://developer.android.com/guide/topics/ui/menus.html ● Source code for SimpleMenuApp for linear browsing of images can be downloaded here ● Chapters 3, 4 in “Professional Android 2 Application Development” by Rito Meier ● Chapter 5 in “Pro Android 2” by Sayed Hashimi, Satya Komatineni, Dave MacLeanhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 27. Feedback Errors, bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com

×