Mobile Application Development 08

933 views

Published on

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
933
On SlideShare
0
From Embeds
0
Number of Embeds
452
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mobile Application Development 08

  1. 1. Mobile Application Development Lecture 08 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Context Menus ● Intentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  3. 3. Context Menushttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  4. 4. Context Menus ● Context menus are available on Windows and Linux via right clicks on UI elements ● Android OS supports context menus via long clicks ● In the emulator, long clicks are mouse clicks held longer than usual clicks ● On Android devices with touchscreens are longer presseshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  5. 5. Context Menu vs. Options Menu ● Context menus may contain a number of menu items and can be associated with listeners ● Main difference b/w options menu and context Menu: options menu is owned by an activity; context menu is owned by a view ● Long clicks/presses can be applied only to views ● An activity may have as many context menus as there are viewshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  6. 6. Context Menus ● While a context menu is owned by a view, the method to populate it belongs to the Activity class: Activity.onCreateContextMenu() ● Activity.onCreateOptionsMenu() is called automatically when the Activity object is created ● Activity.onCreateContextMenu() is not called automatically, because a view does not have to have a context menuhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  7. 7. Context Menus ● If a specific view must have a context menu, the context menu must be explicitly registered with it ● To register a context menu with a view v, you can use Activity.registerForContextMenu(v) ● Context menus do not support shortcuts, icons, or submenushttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  8. 8. Context Menu Create Steps ● Register a view for a context menu in the Activity.onCreate() ● Add menu items to the context menu in Activity.onCreateContextMenu(); this method will not be invoked by Android w/o registration ● Code up responses to context menu clickshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  9. 9. Example ● Develop an app that attaches a context menu to an image view (ImageView) ● The image view displays the image from the Wiki entry about The Conference of the Birds poem by Farrid ud-Din Attar ● The context menu must have two items: Author and Poem ● Selecting Author toasts a short info message about the author ● Selecting Poem toasts a short info message about the poem ● We will call this project ContextMenu01http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  10. 10. ContextMenu01 Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  11. 11. XML Layout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_01" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".ContextMenuAct01" /> <ImageView android:id="@+id/img_view_01" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/conference_of_birds" /> </LinearLayout>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  12. 12. ContextMenuAct01 protected Resources mRes = null; // Resources object protected TextView mTV = null; // TextView for messages protected ImageView mImgView = null; // ImageView // These are the constants we will use in the context menu // attached to mImgView private final int CONTEXT_MENU_AUTHOR_ITEM = 1; private final int CONTEXT_MENU_POEM_ITEM = 2; private final int CONTEXT_MENU_GROUP_ID = ContextMenu.FIRST;http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  13. 13. ContextMenuAct01.onCreate() public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_context_menu_act01); // 1. Get the Resources object mRes = this.getResources(); // 2. Inflate the text view and the image view mTV = (TextView) this.findViewById(R.id.tv_01); mImgView = (ImageView) this.findViewById(R.id.img_view_01); // 3. Attach a context menu to mImgView this.registerForContextMenu(mImgView); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  14. 14. ContextMenuAct01.onContexItemSelectedMenuItem() public boolean onContextItemSelected(MenuItem item) { switch ( item.getItemId() ) { case CONTEXT_MENU_AUTHOR_ITEM: mTV.setText(mRes.getString(R.string.farrid_attar_short_text)); Toast.makeText(this, mRes.getString(R.string.farrid_attar_context_info), Toast.LENGTH_LONG).show(); return true; case CONTEXT_MENU_POEM_ITEM: mTV.setText(mRes.getString(R.string.conference_of_birds_short_text)); Toast.makeText(this, mRes.getString(R.string.conference_of_birds_context_info), Toast.LENGTH_LONG).show(); return true; default: return false; } }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  15. 15. Intentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  16. 16. Overview ● Intents are messages that allow Android components (e.g., activities, applications, etc) to send computational requests to other Android components via Android OS ● Intents are used for requesting information, computation, or event signaling ● All intents objects are instances of android.content.Intenthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  17. 17. Explicit and Implicit Intents ● Intents may contain data: one activity may send data to another activity ● Android supports two types of intents: explicit or implicit ● Explicit intents must know the Java class name of the component that they want to start ● Implicit intents must know the action type they want to be done for themhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  18. 18. Explicit Intents ● If Android OS resolves an explicit intent via its Java class identifier & the component specified by the class identifier is accessible, the component will be launched ● Explicit intents are typically used within one application when the developers have control over the Java class identifiers of the componentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  19. 19. Explicit Intent Example ● Intent i = new Intent(this, ImageDisplayAct01.class); ● If ImageDisplayAct01.class is resolved, the corresponding component is launched ● If not, there is a run-time errorhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  20. 20. Implicit Intents ● Implicit intents must specify the type of action they should perform ● If there is a component in the Android ecosystem that is registered to perform the specified action type, it will be launched ● If there are several components in the Android ecosystem that are registered, Android will ask the user to select one via a selection dialog ● If no component is found, nothing happenshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  21. 21. Implicit Intent Example Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ● If there is a component who can perform MediaStore.ACTION_IMAGE_CAPTURE, it will be launched ● Multiple components may be registered to perform the same actionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  22. 22. Data Transfer with Intents ● Both types of intents can bundle data ● An intent sender creates an intent, put a few key-value pairs into it and send it to the ecosystem ● An intent receiver receives an intent, checks it for the presence/absence of specific keys and then does some action ● Keys are always Stringshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  23. 23. Explicit Intents: Example 01 ● Develop an app that has two Activities: MainLauncher and ImageDisplayerAct01 ● The MainLauncher activity consumes a button click and launches the ImageDisplayerAct01 activity via an explicit intent ● The ImageDisplayerAct01 activity displays a random image with an ImageView object ● We will call this project ExplicitIntentsApp01http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  24. 24. ExplicitIntentsApp01 Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  25. 25. MainLauncherAct.onCreate() protected Button mBtnDisplay = null; protected Activity mThisAct = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_launcher); mBtnDisplay = (Button) this.findViewById(R.id.btn_display); mThisAct = this; // Button click creates an explicit intent to request a launch of ImageDisplayerAct01 mBtnDisplay.setOnClickListener( new OnClickListener() { public void onClick(View v) { // 1. Create an explicit intent Intent i = new Intent(mThisAct, ImageDisplayerAct01.class); // 2. Request Android to run it. startActivity(i); }}); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  26. 26. ImageDisplayerAct01.onCreate() protected ImageView mImgView = null; protected void onCreate(Bundle savedInstanceState) { mImgView = (ImageView) this.findViewById(R.id.img_view_01); mImgView.setImageDrawable(null); int img_num = new Random().nextInt(8) + 1; switch ( img_num ) { case 1: mImgView.setBackgroundResource(R.drawable.img_01); break; case 2: mImgView.setBackgroundResource(R.drawable.img_02); break; // Other cses for 3 to 8 } Toast.makeText(this, getResources().getString(R.string.randome_img_msg) + " " + img_num, Toast.LENGTH_SHORT).show(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  27. 27. Explicit Intents: Example 02 ● Develop an app that has two Activities: MainLauncher and ImageDisplayerAct02 ● The MainLauncher activity consumes a button click and launches the ImageDisplayerAct02 activity via an explicit intent AND passes to the ImageDisplayerAct02 the numerical ID of the image to display ● The ImageDisplayerAct02 activity displays the requested image in an ImageView object ● We will call this project ExplicitIntentsApp02http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  28. 28. ExplicitIntentsApp02 Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  29. 29. MainLauncherAct.onCreate() mBtnDisplay.setOnClickListener( new OnClickListener() { public void onClick(View v) { // 1. Create an explicit intent Intent i = new Intent(mThisAct, ImageDisplayerAct02.class); // 2. Generate a random number in [1, 8] int random_img_id = new Random().nextInt(8) + 1; // 3. Put key-value pair into the intent. i.putExtra(mRes.getString(R.string.img_id_key), random_img_id); // 4. Toast which image is requested. Toast.makeText(mThisAct, mRes.getString(R.string.img_display_request_msg) + " " + random_img_id, Toast.LENGTH_SHORT).show(); // 5. Request Android to run it. startActivity(i);}}); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  30. 30. ImageDisplayerAct02.onCreate() // 1. Get the intent Intent i = this.getIntent(); // 2. Check to see if it contains an integer keyed on R.string.img_id_key. // default to 0 if it does not. int img_num = i.getIntExtra(mRes.getString(R.string.img_id_key), 0); // 3. If it does, display it if (img_num > 0) { switch (img_num) { case 1: mImgView.setBackgroundResource(R.drawable.img_01); break; case 2: mImgView.setBackgroundResource(R.drawable.img_02); break; // handle other cases from 3 to 8 } // Set the text of the mTV text view to the appropriate message. mTV.setText(mRes.getString(R.string.img_display_msg) + " " + img_num);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  31. 31. References ● developer.android.com/guide/topics/ui/menus.html ● http://developer.android.com/guide/components/intents-filters.htmlhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  32. 32. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com

×