Menu tips

From Android Wiki

(Difference between revisions)
Jump to: navigation, searcha
 
Line 1: Line 1:
An Android [http://developer.android.com/reference/android/app/Activity.html Activity] can have an options menu (brought up by pressing the standard Menu button) and a context menu (commonly triggered by long-tapping on something). The options menu is created by your activity defining an <TT>onCreateOptionsMenu</TT> callback, while the context menu is set up in your <TT>onCreateContextMenu</TT> callback.
An Android [http://developer.android.com/reference/android/app/Activity.html Activity] can have an options menu (brought up by pressing the standard Menu button) and a context menu (commonly triggered by long-tapping on something). The options menu is created by your activity defining an <TT>onCreateOptionsMenu</TT> callback, while the context menu is set up in your <TT>onCreateContextMenu</TT> callback.
-
Conventional ways of managing these menus might be something like this: '''Source(s):'''  [http://www.downloadranking.com  Android solutions]
+
Conventional ways of managing these menus might be something like this:
<code>
<code>
Line 109: Line 109:
</code>
</code>
-
Instead of these, a less housekeeping-intensive way is to set up a table of menu items and associated actions, like this: '''Source(s):'''  [http://www.downloadranking.com  Android solutions]
+
Instead of these, a less housekeeping-intensive way is to set up a table of menu items and associated actions, like this:
<code>
<code>
Line 175: Line 175:
[[Category:Programming]]
[[Category:Programming]]
-
 
-
'''Source(s):'''  [http://www.downloadranking.com  Android solutions]
 

Latest revision as of 08:44, 31 January 2013

An Android Activity can have an options menu (brought up by pressing the standard Menu button) and a context menu (commonly triggered by long-tapping on something). The options menu is created by your activity defining an onCreateOptionsMenu callback, while the context menu is set up in your onCreateContextMenu callback.

Conventional ways of managing these menus might be something like this:

   protected android.view.MenuItem DoStuffItem1;
   protected android.view.MenuItem DoStuffItem2;
   ... more MenuItem objects as necessary ...
    
   ...
    
   @Override
   public boolean onCreateOptionsMenu
     (
       android.view.Menu TheMenu
     )
     {
       DoStuffItem1 = TheMenu.add(R.string.do_stuff_1);
       DoStuffItem2 = TheMenu.add(R.string.do_stuff_2);
       ... add more menu items ...
       return
           true;
     } /*onCreateOptionsMenu*/
    
   ...
    
   @Override
   public boolean onOptionsItemSelected
     (
       android.view.MenuItem TheItem
     )
     {
       boolean Handled = false;
       if (TheItem == DoStuffItem1)
         {
           ... do stuff 1 ...
           Handled = true;
         }
       else if (TheItem == DoStuffItem2)
         {
           ... do stuff 2 ...
           Handled = true;
         }
       ... perform actions for remaining menu items ...
       return
           Handled;
     } /*onOptionsItemSelected*/
    

Or even like this:

   protected static final int DoStuffItem1 = 1;
   protected static final int DoStuffItem2 = 2;
   ... more codes as necessary for more menu items ...
    
   ...
    
   @Override
   public boolean onCreateOptionsMenu
     (
       android.view.Menu TheMenu
     )
     {
       TheMenu.add
         (
           /*groupId =*/ android.view.Menu.NONE,
           /*itemId =*/ DoStuffItem1,
           /*order =*/ android.view.Menu.NONE,
           /*titleRes =*/ R.string.do_stuff_1
         );
       TheMenu.add
         (
           /*groupId =*/ android.view.Menu.NONE,
           /*itemId =*/ DoStuffItem2,
           /*order =*/ android.view.Menu.NONE,
           /*titleRes =*/ R.string.do_stuff_2
         );
       ... add more menu items ...
       return
           true;
     } /*onCreateOptionsMenu*/
    
   ...
    
   @Override
   public boolean onOptionsItemSelected
     (
       android.view.MenuItem TheItem
     )
     {
       boolean Handled = false;
       switch (TheItem.getItemId())
         {
       case DoStuffItem1:
           ... do stuff 1 ...
           Handled = true;
       break;
       case DoStuffItem2:
           ... do stuff 2 ...
           Handled = true;
       break;
       ... perform actions for remaining menu items ...
         } /*switch*/
       return
           Handled;
     } /*onOptionsItemSelected*/

Instead of these, a less housekeeping-intensive way is to set up a table of menu items and associated actions, like this:

   protected java.util.Map<android.view.MenuItem, Runnable> OptionsMenu;
    
   ...
    
   @Override
   public boolean onCreateOptionsMenu
     (
       android.view.Menu TheMenu
     )
     {
       OptionsMenu = new java.util.HashMap<android.view.MenuItem, Runnable>();
       OptionsMenu.put
         (
           TheMenu.add(R.string.do_stuff_1),
           new Runnable()
             {
               public void run()
                 {
                   ... do stuff 1 ...
                 } /*run*/
             } /*Runnable*/
         );
       OptionsMenu.put
         (
           TheMenu.add(R.string.do_stuff_2),
           new Runnable()
             {
               public void run()
                 {
                   ... do stuff 2 ...
                 } /*run*/
             } /*Runnable*/
         );
       ... define more menu items and actions ...
       return
           true;
     } /*onCreateOptionsMenu*/
    
   ...
    
   @Override
   public boolean onOptionsItemSelected
     (
       android.view.MenuItem TheItem
     )
     {
       boolean Handled = false;
       final Runnable Action = OptionsMenu.get(TheItem);
       if (Action != null)
         {
           Action.run();
           Handled = true;
         } /*if*/
       return
           Handled;
     } /*onOptionsItemSelected*/

Notice how all the menu item actions are completely defined in onCreateOptionsMenu, and there is no need to manage separate lists of MenuItem objects or assigned menu item IDs—all the necessary information is tracked in the OptionsMenu table, and the onOptionsItemSelected method becomes a complete, simple, reusable routine. Adding more menu items is a simple matter of adding more OptionsMenu.put calls in onCreateOptionsMenu.

The context menu can be managed in the same way, with its own Map<MenuItem, Runnable> table.

Personal tools