Android Adventures - Menus - The Toolbar
Written by Mike James   
Tuesday, 05 May 2015
Article Index
Android Adventures - Menus - The Toolbar
Action Bar
Responding to menu events
Controlling the Action Bar

Controlling The Action Bar

There are a couple of questions that still remain. You can see how to add items, submenus and generally control what the Toolbar displays but what about controlling when it is displayed or how it look. 

The answer to these question is based on working with the ActionBar object and as you might expect this is not something general to all menus. The ActionBar has lots of features for showing menus in different ways and to cover it completely would take too much space and it needs a chapter all to itself. 

However it is worth knowing some of the basic customizations that you can apply. 

You can get the ActionBar object in an Activity using getSupportActionBar or getActionBar. Once you have the ActionBar object you can use its show() and hide() methods to remove and show it as required:

ActionBar actionBar=getSupportActionBar();
actionBar.hide();

Similarly you can change the title and subtitle displayed in the action bar:

ActionBar actionBar=getSupportActionBar();
actionBar.setTitle("My Action Bar");
actionBar.setSubtitle("My Subtitle");

 

Myactiona

 

There are many other properties and methods that you can use to modify the way the action bar looks and behaves. 

Things that are worth looking up are:

  • using a split action bar
  • Up Navigation
  • Action views
  • collapsible action views
  • action providers
  • navigation tabs

 

Summary

  • A menu is a hierarchy of View objects just like any UI element.

  • You can create a menu in code but using a menu XML resource file is the most common way of doing the job. 

  • There are Menu objects and tags which act as containers for Item objects and tags. 

  • Submenus can be created by nesting Menu objects within other Menu objects. 

  • Menu items can have a title, icon, and many other attributes which govern how they are displayed. 

  • There are four different ways you can display a menu:
    action bar/toolbar, context menu,contextual action mode and popup.

  • To make use of the action bar/toolbar and the contextual action mode in earlier versions of Android you need to use the AppCompatActivity and the other support classes

  • To display an action bar/toolbar all you have to do is use the onOptionsCreate event handler to create a menu, usually by inflating a menu resource. 

  • In general you can handle click events on each menu item or you can make use of an items click event handler that the system provides that responds to a click on any of the items. It is usual to use the items click event handler. 

  • For the action bar/Toolbar the items click event handler is defined in the Activity as  onOptionsItemSelected(MenuItem item)

Where Next?

The subject of menus and how to use them is a big one and this is really just a basic introduction to the idea. From here you can learn about how to group menu items together, how to put check boxes and even completely general View objects into menus. Then there is the whole subject of using context menus and popup menus which is the subject of the next chapter.

 

You can download the code for this program from the CodeBin (note you have to register first).

 

androidJavaSmallAndroid Programming In Java:
Starting With an App
Third Edition

Is now available in paperback and ebook.

Available from Amazon.

 

 

  1. Getting Started With Android Studio 3
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Android Events
         Extract: Using Lambdas
  5. Basic Controls
  6. Layout Containers
  7. The ConstraintLayout
        Extract: Guidelines and Barriers
  8. UI Graphics A Deep Dive
        Extract: Programming the UI ***NEW
  9. Menus & The Action Bar
  10. Menus, Context & Popup
  11. Resources
  12. Beginning Bitmap Graphics
        Extract: Simple Animation
  13. Staying Alive! Lifecycle & State
  14. Spinners
  15. Pickers
  16. ListView And Adapters

If you are interested in creating custom template also see:

Custom Projects In Android Studio

Androidgears

 

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 



Last Updated ( Tuesday, 14 March 2017 )