Wednesday, May 16, 2012

12. OptionMenu and ActionBar

12.1. ActionBar

The ActionBar is located at the top of the Activity that may display the Activity title, navigation modes, and other interactive items.
The following picture show the ActionBar of a typical Google Application with interactive items and a nagivation bar.
ActionBar Screenshot

12.2. OptionsMenu

The application can also open a menu which shows actions via a popup menu. This OptionsMenu is only available if the phone has a hardware "Options" button. Even if the hardware button is available, it is recommended to use the ActionBar, which is available for phones as of Android 4.0.
The following picture highlights the hardware button and the resulting menu as popup.
Old OptionsMenu
One of the reasons why the ActionBar is superior to the OptionsMenu, if that it is clearly visible, while the OptionsMenu is only shown on request and the user may not recognize that options are available.

12.3. Creating the menu

The OptionsMenu and the ActionBar is filled by the onCreateOptionsMenu() method of your Activity.
In the onCreateOptionsMenu() method you can create the menu entries. You can add menu entries via code or via the inflation of an existing XML resources.
The MenuInflator class allows to inflate menu entries defined in XML to the menu. MenuInflator can get accessed via the getMenuInflator() method in your Activity.
The onCreateOptionsMenu() method is only called once. If you want to influence the menu later you have to use the onPrepareOptionsMenu() method. onPrepareOptionsMenu() is not called for entries in the ActionBar for these entries you have to use the invalidateOptionsMenu() method.

12.4. Reacting to menu entry selection

If a menu entry is selected then then onOptionsItemSelected() method is called. As parameter you receive the menu entry which was selected so that you can react differently to different menu entries.

12.5. Using the home icon

The ActionBar also shows an icon of your application. You can also add an action to this icon. If you select this icon the onOptionsItemSelected() method will be called with the value android.R.id.home. The recommendation is to return to the main Activity in your program.
    
// If home icon is clicked return to main Activity
case android.R.id.home:
 Intent intent = new Intent(this, OverviewActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(intent);
 break;
   

12.6. ActionBar tabs

It is also possible to add tabs to the ActionBar which can be used for navigation. Typically Fragments are used for this purpose. We demonstrate this in the Fragments chapter.

12.7. Custom Views in the ActionBar

You can also add a custom View to the ActionBar. The following code snippet demonstrates that.
    
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 
 ActionBar actionBar = getActionBar();
 // add the custom view to the action bar
 actionBar.setCustomView(R.layout.actionbar_view);
 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
  | ActionBar.DISPLAY_SHOW_HOME);
}
   

12.8. Contextual action mode

A contextual action mode activates a temporary ActionBar that overlays the application ActionBar for the duration of a particular sub-task.
The contextual action mode is typically activated by selecting an item or by long clicking on it.
To implemented this, call the startActionMode() method on a View or on your Activity. This method gets an ActionMode.Callback object which is responsible for the lifecycle of the contextual ActionBar.

12.9. Context menus

You can also assign a context menu to a View. A context menu is also activated if the user "long presses" the view.
If possible the contextual action mode should be preferred over a context menu.
A context menu for a view is registered via the registerForContextMenu(view) method. The onCreateContextMenu() method is called every time a context menu is activated as the context menu is discarded after its usage. The Android platform may also add options to your View, e.g. EditText provides context options to select text, etc.

No comments:

Post a Comment