• Save
Android Programming: Lecture 05
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Android Programming: Lecture 05

on

  • 2,429 views

1) Intent Broadcasting...

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

Statistics

Views

Total Views
2,429
Views on SlideShare
1,258
Embed Views
1,171

Actions

Likes
0
Downloads
0
Comments
0

35 Embeds 1,171

http://vkedco.blogspot.com 667
http://vkedco.blogspot.de 191
http://vkedco.blogspot.in 124
http://vkedco.blogspot.fr 30
http://vkedco.blogspot.co.uk 16
http://vkedco.blogspot.co.il 14
http://vkedco.blogspot.ca 13
http://vkedco.blogspot.ro 11
http://vkedco.blogspot.cz 9
http://vkedco.blogspot.com.es 9
http://vkedco.blogspot.se 7
http://vkedco.blogspot.nl 7
http://vkedco.blogspot.mx 6
http://vkedco.blogspot.gr 6
http://vkedco.blogspot.com.tr 5
http://vkedco.blogspot.com.au 5
http://vkedco.blogspot.it 5
http://vkedco.blogspot.fi 4
http://vkedco.blogspot.ie 4
http://vkedco.blogspot.sk 4
http://vkedco.blogspot.jp 3
http://vkedco.blogspot.kr 3
http://vkedco.blogspot.hk 3
http://www.vkedco.blogspot.com 3
http://vkedco.blogspot.ch 3
http://vkedco.blogspot.co.nz 3
http://vkedco.blogspot.hu 2
http://vkedco.blogspot.tw 2
http://vkedco.blogspot.sg 2
http://vkedco.blogspot.co.at 2
http://vkedco.blogspot.com.br 2
http://vkedco.blogspot.com.ar 2
http://vkedco.blogspot.pt 2
http://vkedco.blogspot.ru 1
http://www.vkedco.blogspot.in 1
More...

Accessibility

Categories

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

Android Programming: Lecture 05 Presentation Transcript

  • 1. Android Programming Lecture 05 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. Intent Broadcast and Receptionwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. 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. 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. 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. Fibonacci Sum Systemwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. 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. 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. 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. 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. 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. 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. Nth Fibonacci Sum Projects Source Code FNSConsumer2, FNSProviderIter2, FNSProviderRec2www.youtube.com/vkedco www.vkedco.blogspot.com
  • 20. Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. 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. 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. 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. 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. 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. 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. Responding to Menu Eventswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. 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. 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. 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. Setting Up Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. MenuHarness Projects Source Codewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. MenuHarness Projects Source Code Main and Secondary Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. MenuHarness Projects Source Code Icon Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 45. MenuHarness Projects Source Code SubMenuswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. 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. 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. MenuHarness Projects Source Code Context Menuswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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. 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. Feedback Bugs, errors to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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