• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Mobile Application Development: Lecture 07
 

Mobile Application Development: Lecture 07

on

  • 906 views

 

Statistics

Views

Total Views
906
Views on SlideShare
483
Embed Views
423

Actions

Likes
1
Downloads
0
Comments
0

25 Embeds 423

http://vkedco.blogspot.com 232
http://www.vkedco.blogspot.com 119
http://vkedco.blogspot.in 21
http://vkedco.blogspot.co.uk 15
http://vkedco.blogspot.co.il 4
http://www.vkedco.blogspot.in 4
http://vkedco.blogspot.hu 3
http://vkedco.blogspot.com.es 2
http://vkedco.blogspot.com.ar 2
http://www.vkedco.blogspot.co.il 2
http://vkedco.blogspot.kr 2
http://vkedco.blogspot.ca 2
http://vkedco.blogspot.de 2
http://vkedco.blogspot.pt 2
http://vkedco.blogspot.nl 1
http://vkedco.blogspot.com.au 1
http://vkedco.blogspot.com.tr 1
http://vkedco.blogspot.ch 1
http://www.blogger.com 1
http://vkedco.blogspot.it 1
http://www.vkedco.blogspot.gr 1
http://vkedco.blogspot.be 1
http://vkedco.blogspot.se 1
http://vkedco.blogspot.fr 1
http://www.vkedco.blogspot.ru 1
More...

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Mobile Application Development: Lecture 07 Mobile Application Development: Lecture 07 Presentation Transcript

    • Mobile Application Development Lecture 07 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Outline ● Menus ● Key Menu Classes ● Menu Construction ● Grouping Menu Items ● Responding to Menu Item Selectionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • 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
    • Menu Constructionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • 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
    • 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
    • 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
    • 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
    • SimpleMenuApphttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Gus Khrustalny (Chrystal Goose), Russia Source: http://images.yandex.ruhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • 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
    • 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
    • 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
    • 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
    • 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
    • Grouping Menu Itemshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • 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
    • 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
    • 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
    • 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
    • Responding to Menu Item Selectionshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • 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
    • 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
    • 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
    • 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
    • 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
    • Feedback Errors, bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com