Android Programming                         Lecture 05                       Vladimir Kulyukin                Department o...
Resources   Source code and presentation materials for this lecture   are available at   http://vkedco.blogspot.com/2011/1...
Outline   ●     Intent Broadcast   ●     Intent Reception   ●     Menus: Main Menus, Secondary menus,     Options Menus, S...
Intent Broadcast and Receptionwww.youtube.com/vkedco         www.vkedco.blogspot.com
Intent Broadcast and Reception   ●     Let us implement a simple broadcast system   ●     There is one application that ne...
Intent Broadcast and Reception   ●     Assumption 1: The first application does not     know of the existence of the other...
Intent Broadcast and Reception   ●     The first app broadcasts its request to     compute the sum of the first n Fibonacc...
Intent Resolution   ●     What happens after the first application     broadcasts its request through an intent?   ●     A...
Intent Broadcast and Reception   ●     The first app also registers itself with Android     as a broadcast receiver to rec...
Fibonacci Sum Systemwww.youtube.com/vkedco           www.vkedco.blogspot.com
Fibonacci Sum System   ●       FNSConsumer2 – broadcasts requests to compute       the n-th Fibonacci sum and receives eve...
Fibonacci Sum System   ●       FNSConsumer2 – in addition, this application can       use three criteria: BEAUTY, EFFICIEN...
Fibonacci Sum System: FNSConsumer2 ●     FNSConsumer2 – broadcasts the following intents: ●     Intent {       action = “o...
Fibonacci Sum System: FNSConsumer2 ●     FNSConsumer2 – receives the following intent: ●     Intent {       action = “org....
Fibonacci Sum System: FNSProviderIter2 ●     FNSProviderIter2 – receives the following intent: ●     Intent {       action...
Fibonacci Sum System: FNSProviderIter2 ●     FNSProviderIter2 – broadcasts the following intents: ●     Intent {       act...
Fibonacci Sum System: FNSProviderRec2 ●     FNSProviderRec2 – receives the following intents: ●     Intent {       action ...
Fibonacci Sum System: FNSProviderIter2 ●     FNSProviderRec2 – broadcasts the following intents: ●     Intent {       acti...
Nth Fibonacci Sum Projects Source                       Code         FNSConsumer2, FNSProviderIter2,               FNSProv...
Menuswww.youtube.com/vkedco       www.vkedco.blogspot.com
Android Menu Classes    ●        The key classes:           – android.view.Menu           – android.view.SubMenu          ...
Grouping Menu Items    ●        Menu items can be grouped by assigning each item a        group ID    ●        Multiple me...
Menu Item Attributes    ●        Menu items also have a title, an ID, and a sort-order        ID (number)    ●        The ...
Menu Creation    ●        Activity is always associated with a single Menu    ●        Such menus in Android are also call...
onCreateOptionsMenu()        public boolean onCreateOptionsMenu(Menu menu) {              // populate menu items          ...
onCreateOptionsMenu()      public boolean onCreateOptionsMenu(Menu menu) {             super.onCreateOptionsMenu(menu);   ...
Menu Group Construction      public boolean onCreateOptionsMenu(Menu menu) {             int gid1 = 1;             menu.ad...
Menu Group Manipulation    ●        removeGroup(gid) – removes all menu items from        that group    ●        setGroupC...
Responding to Menu Eventswww.youtube.com/vkedco          www.vkedco.blogspot.com
Three Response Options    ●        onOptionsItemSelected() – override this        method for the Activity class    ●      ...
Response 1: onOptionsItemSelected()       @Override       public boolean onCreateOptionsMenu(Menu mi) {              switc...
Response 1: onOptionsItemSelected()       @Override       public boolean onCreateOptionsMenu(Menu mi) {              switc...
Response 2: OnMenuClickListener     public class MyResponse implements OnMenuClickListener {            public boolean onM...
Response 3: Intent    ●        Either do not override onOptionsItemSelected()        or override it and invoke the parent ...
Setting Up Menuswww.youtube.com/vkedco             www.vkedco.blogspot.com
Programmatic Menu Setup    ●        Create an XML layout for the Activity    ●        Create the Activity that hosts the l...
Programmatic Menu Setup 2    ●        If necessary, add secondary menu items        to the menu    ●        If necessary, ...
MenuHarness Projects Source Codewww.youtube.com/vkedco       www.vkedco.blogspot.com
Menu Harness: strings.xml   <?xml version="1.0" encoding="utf-8"?>   <resources>     <string name="hello">Hello World, Men...
Menu Harness: main.xml   <?xml version="1.0" encoding="utf-8"?>   <LinearLayout xmlns:android="http://schemas.android.com/...
MenuHarness Projects Source Code                 Main and Secondary Menuswww.youtube.com/vkedco         www.vkedco.blogspo...
Other Menu Types    ●        In addition to main and secondary menus,        Android supports four other menu types:      ...
Icon Menus    ●        Icons can be used to represent menu items        instead of or in addition to text    ●        Expa...
MenuHarness Projects Source Code                         Icon Menuswww.youtube.com/vkedco          www.vkedco.blogspot.com
MenuHarness Projects Source Code                         SubMenuswww.youtube.com/vkedco         www.vkedco.blogspot.com
Context Menus    ●        Context menus are standard on other        operating systems (right click on        Windows/Linu...
Context Menus    ●        Context menus may contain a number of        menu items and associated with listeners    ●      ...
Context Menus    ●        While a context menu is owned by a view, the        method to populate it resides in the Activit...
Context Menu Creation    ●        Register a view for a context menu in the        activitys onCreate()    ●        Add me...
MenuHarness Projects Source Code                         Context Menuswww.youtube.com/vkedco           www.vkedco.blogspot...
Alternative Menus    ●        Alternative menus allow multiple applications        on Android to use each other    ●      ...
Alternative Menu Setup    ●        Create an intent with a URI to the data    ●        Set the intents category to        ...
Feedback      Bugs, errors to vladimir dot kulyukin at gmail      dot comwww.youtube.com/vkedco             www.vkedco.blo...
References & Reading Suggestions    ●        http://developer.android.com/guide/topics/ui/menus.html    ●        http://de...
Upcoming SlideShare
Loading in...5
×

Android Programming: Lecture 05

2,087

Published on

1) Intent Broadcasting
2) Intent Reception
3) Menus: Main Menus, Secondary Menus, Options Menus, Submenus, Context Menus
4) Responding to Menu Events
5) Menu construction and setup
6) Class homepage is at http://vkedco.blogspot.com/2011/08/android-programming-home.html

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

  • Be the first to like this

No Downloads
Views
Total Views
2,087
On Slideshare
0
From Embeds
0
Number of Embeds
29
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Android Programming: Lecture 05

  1. 1. Android Programming Lecture 05 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Resources Source code and presentation materials for this lecture are available at http://vkedco.blogspot.com/2011/10/android-programming-lecture-07.htmlwww.youtube.com/vkedco
  3. 3. Outline ● Intent Broadcast ● Intent Reception ● Menus: Main Menus, Secondary menus, Options Menus, Submenus, Context Menus ● Menu Construction ● Responding to Menu Events ● Menu Setupwww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Intent Broadcast and Receptionwww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. Intent Broadcast and Reception ● Let us implement a simple broadcast system ● There is one application that needs to compute the sum of the first n Fibonacci numbers ● There are two other applications that can compute the sum of the first n Fibonacci numberswww.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. Intent Broadcast and Reception ● Assumption 1: The first application does not know of the existence of the other two applications so it cannot invoke them explicitly ● Assumption 2: The second and third applications do not know of the first onewww.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. Intent Broadcast and Reception ● The first app broadcasts its request to compute the sum of the first n Fibonacci numbers – it is a broadcaster ● The second and third apps register themselves as broadcast receivers that respond to the specific type of request to compute the sum of the first n Fibonacci numberswww.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Intent Resolution ● What happens after the first application broadcasts its request through an intent? ● Android does intent resolution and invokes the second and/or third applications ● After the second or third broadcast receiver does its computation, it can create a result intent and broadcast it back to Androidwww.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Intent Broadcast and Reception ● The first app also registers itself with Android as a broadcast receiver to receive the events of that announce the computation of the n-th Fibonacci sum ● The Android invokes that receiver and it updates the user interfacewww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. Fibonacci Sum Systemwww.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Fibonacci Sum System ● FNSConsumer2 – broadcasts requests to compute the n-th Fibonacci sum and receives events that contain results of that computation ● FNSProviderIter2 – receives broadcasts to compute the n-th Fibonacci sum and broadcasts the result computed iteratively ● FNSProviderRec2 – receives broadcasts to compute the n-th Fibonacci sum and broadcasts the result computed recursivelywww.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Fibonacci Sum System ● FNSConsumer2 – in addition, this application can use three criteria: BEAUTY, EFFICIENCY, BOTH ● FNSProviderIter2 – responds to broadcasts for the n-th Fibonacci sum when EFFICIENCY or BOTH are specified ● FNSProviderRec2 – responds to broadcasts for the n- th Fibonacci sum when BEAUTY or BOTH are specifiedwww.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. Fibonacci Sum System: FNSConsumer2 ● FNSConsumer2 – broadcasts the following intents: ● Intent { action = “org.vkedco.android.intents.COMPUTE_FIB_SUM” extras = Bundle { <N, 5> <CRITERION, BEAUTY> } }www.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Fibonacci Sum System: FNSConsumer2 ● FNSConsumer2 – receives the following intent: ● Intent { action = “org.vkedco.android.intents.FIB_SUM_COMPUTED” extras = Bundle { <FIB_SUM, 12> <SOURCE, FNSProviderIter2> } }www.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Fibonacci Sum System: FNSProviderIter2 ● FNSProviderIter2 – receives the following intent: ● Intent { action = “org.vkedco.android.intents.COMPUTE_FIB_SUM” extras = Bundle { <N, 5> <CRITERION, CRITERION-VALUE> } }www.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Fibonacci Sum System: FNSProviderIter2 ● FNSProviderIter2 – broadcasts the following intents: ● Intent { action = “org.vkedco.android.intents.FIB_SUM_COMPUTED” extras = Bundle { <FIB_SUM, 12> <SOURCE, FNSProviderIter2> } }www.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. Fibonacci Sum System: FNSProviderRec2 ● FNSProviderRec2 – receives the following intents: ● Intent { action = “org.vkedco.android.intents.COMPUTE_FIB_SUM” extras = Bundle { <N, 5> <CRITERION, CRITERION-VALUE> } }www.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. Fibonacci Sum System: FNSProviderIter2 ● FNSProviderRec2 – broadcasts the following intents: ● Intent { action = “org.vkedco.android.intents.FIB_SUM_COMPUTED” extras = Bundle { <FIB_SUM, 12> <SOURCE, FNSProviderRec2> } }www.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. Nth Fibonacci Sum Projects Source Code FNSConsumer2, FNSProviderIter2, FNSProviderRec2www.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. Android 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 MenuItemswww.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Grouping Menu Items ● 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 provides a set of methods that take group IDs and enable a specific functionality for all menu items in a selected groupwww.youtube.com/vkedco www.vkedco.blogspot.com
  23. 23. Menu Item Attributes ● Menu items also have a title, an ID, and a sort-order ID (number) ● The sort-order ID is used to specify the order of menu items within a menu ● A menu item whose sort-order ID is 2 appears above a menu item whose sort-order ID is 4www.youtube.com/vkedco www.vkedco.blogspot.com
  24. 24. Menu Creation ● Activity is always associated with a single Menu ● Such menus in Android are also called options menus ● Android creates this Menu automatically and passes it to the onCreateOptionsMenu callback method ● The method allows the programmer to populate the created Menu with MenuItemswww.youtube.com/vkedco www.vkedco.blogspot.com
  25. 25. onCreateOptionsMenu() public boolean onCreateOptionsMenu(Menu menu) { // populate menu items return true } ● If the method returns true, the menu is made visible ● If the method returns false, the menu is made invisiblewww.youtube.com/vkedco www.vkedco.blogspot.com
  26. 26. onCreateOptionsMenu() public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); // 0 – group ID, 1 – item id, 2 – sort-order ID, // “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 }www.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. Menu Group Construction public boolean onCreateOptionsMenu(Menu menu) { int gid1 = 1; menu.add(gid1, 1, 1, “group 1 item 1”); menu.add(gid2, 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 }www.youtube.com/vkedco www.vkedco.blogspot.com
  28. 28. 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)www.youtube.com/vkedco www.vkedco.blogspot.com
  29. 29. Responding to Menu Eventswww.youtube.com/vkedco www.vkedco.blogspot.com
  30. 30. 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)www.youtube.com/vkedco www.vkedco.blogspot.com
  31. 31. Response 1: onOptionsItemSelected() @Override public boolean onCreateOptionsMenu(Menu mi) { switch ( mi.getItemId() ) { ... } return true; } ● For the menu items that are handled, this method should return truewww.youtube.com/vkedco www.vkedco.blogspot.com
  32. 32. Response 1: onOptionsItemSelected() @Override public boolean onCreateOptionsMenu(Menu mi) { switch ( mi.getItemId() ) { ... } return super.onOptionsItemSelected(item); } ● For the menu items that are not handled, this method refer to superwww.youtube.com/vkedco www.vkedco.blogspot.com
  33. 33. Response 2: OnMenuClickListener public class MyResponse implements OnMenuClickListener { public boolean onMenuItemClick(MenuItem item) { …. } } MyResponse myRes = new MyResponse(...) menu.setOnItemClickListener(myRes)www.youtube.com/vkedco www.vkedco.blogspot.com
  34. 34. Response 3: Intent ● Either do not override onOptionsItemSelected() or override it and invoke the parent for the menu items that are not handled ● If you do not invoke the parent, the intents will not be handled properlywww.youtube.com/vkedco www.vkedco.blogspot.com
  35. 35. Setting Up Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  36. 36. Programmatic Menu Setup ● Create an XML layout for the Activity ● Create the Activity that hosts the layout ● Set up the menu ● Add regular menu items to the menu ● Add responses to menu itemswww.youtube.com/vkedco www.vkedco.blogspot.com
  37. 37. Programmatic Menu Setup 2 ● If necessary, add secondary menu items to the menu ● If necessary, add responses to secondary menu items ● If necessary, add context menus ● If necessary, add responses to context menu itemswww.youtube.com/vkedco www.vkedco.blogspot.com
  38. 38. MenuHarness Projects Source Codewww.youtube.com/vkedco www.vkedco.blogspot.com
  39. 39. Menu Harness: strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MenuHarnessAct!</string> <string name="app_name">Menu Harness</string> <string name="show_sec_menu_flag">true</string> <string name="show_icon_flag">false</string> <string name="show_submenu_flag">false</string> <string name="show_submenu_icon">false</string> <string name="show_context_menu">false</string> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  40. 40. Menu Harness: main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/messageBoard" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout>www.youtube.com/vkedco www.vkedco.blogspot.com
  41. 41. MenuHarness Projects Source Code Main and Secondary Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  42. 42. Other Menu Types ● In addition to main and secondary menus, Android supports four other menu types: – Icon menus – Submenus – Context menus – Alternative menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  43. 43. Icon Menus ● Icons can be used to represent menu items instead of or in addition to text ● Expanded menus do not support icons ● Icon menu items do not support menu item check marks ● To associate an icon with a menu item, use MenuItem.setIcon ● Icons must be saved in /res/drawablewww.youtube.com/vkedco www.vkedco.blogspot.com
  44. 44. MenuHarness Projects Source Code Icon Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  45. 45. MenuHarness Projects Source Code SubMenuswww.youtube.com/vkedco www.vkedco.blogspot.com
  46. 46. Context Menus ● Context menus are standard on other operating systems (right click on Windows/Linux) ● Android supports context menus with long clicks ● A long click is supported through a long touch, a long ball press, a long mouse click in the emulatorwww.youtube.com/vkedco www.vkedco.blogspot.com
  47. 47. Context Menus ● Context menus may contain a number of menu items and associated with listeners ● Main difference b/w Menu and Context Menu: Menu (options menu) is owned by an Activity; Context Menu is owned by a View ● Long clicks are applied to Views to start their Context Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  48. 48. Context Menus ● While a context menu is owned by a view, the method to populate it resides in the Activity class: activity.onCreateContextMenu() ● Unlike activity.onCreateOptionsMenu(), activity.onCreateContextMenu() is not called automatically ● A view that owns a context menu must register with its activity for context menu ownershipwww.youtube.com/vkedco www.vkedco.blogspot.com
  49. 49. Context Menu Creation ● Register a view for a context menu in the activitys onCreate() ● Add menu items to the context menu in onCreateContextMenu(); this method will not be invoked by Android w/o registration ● Code up responses to context menu clickswww.youtube.com/vkedco www.vkedco.blogspot.com
  50. 50. MenuHarness Projects Source Code Context Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  51. 51. Alternative Menus ● Alternative menus allow multiple applications on Android to use each other ● These menus are part of the Android inter- application communication framework ● Alternative menus allow one application to include menus of another application ● Target application or activity is launched with a URL to the data it requireswww.youtube.com/vkedco www.vkedco.blogspot.com
  52. 52. Alternative Menu Setup ● Create an intent with a URI to the data ● Set the intents category to CATEGORY_ALTERNATIVE ● Search for activities that support the URIs data type ● Add intents that invoke those activities to the menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  53. 53. Feedback Bugs, errors to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com
  54. 54. References & Reading Suggestions ● http://developer.android.com/guide/topics/ui/menus.html ● http://developer.android.com/guide/topics/intents/intents-filters.html ● Chapters 3, 4 in “Professional Android 2 Application Development” by Rito Meier ● Chapter 5 in “Pro Android 2” by Sayed Hashimi, Satya Komatineni, Dave MacLeanwww.youtube.com/vkedco www.vkedco.blogspot.com

×