ActionBar and Toolbar


The action bar, also known as the app bar, is a design element, mainly for:
  • Gives your app an identity and indicates the user's location in the app.
  • Access to important actions in a predictable way, such as search.
  • Support for navigation and view switching (with tabs or drop-down lists).
Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window and are controlled by the Android framework. Since app bar features have gradually been added to the native ActionBar over various Android releases, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library's version of Toolbar (introduced on Android L (API level 21)), and they are available on any device that can use the support library.

For this reason, you should use the support library's Toolbar class to implement your activities' app bars. Using the support library's toolbar helps ensure that your app will have consistent behavior across the widest range of devices. 

Toolbar

The toolbar is a view defined in its activity's layout. Developer may signal to the Activity which Toolbar should be treated as the Activity's action bar, calling the setSupportActionBar() method on Activity's onCreate() lifecycle callback. Activities that use this feature should use one of the supplied .NoActionBar themes, set the windowActionBar attribute to false or otherwise not request the window feature.

Typical components of a toolbar

  • A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations.
  • A branded logo image.
  • A title and subtitle. The title should indicates the users's current destination in navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
  • One or more views. The application may add arbitrary child views to the Toolbar. For example, we can have a EditText widget on our toolbar.
  • An ActionMenuView. The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions and an optional overflow menu for additional actions.

How to implements a Toolbar as ActionBar?

  1. Add the AndroidX (the new version of Support Library) to your project.
  2. Make each activity that uses toolbar as ActionBar to extends AppCompatActivity class.
  3. In the app manifest, set the <application> element to use one of appcompat's NoActionBar themes or a theme that has one of appcompat's NoActionBar themes as parent.
  4. Add a Toolbar to the activity's layout. Position the toolbar at the top of the activity's layout, since you are using it as an app bar.
  5. In the activity's onCreate() method, call the activity's setSupportActionBar() method, and pass the activity's toolbar. This method sets the toolbar as the app bar for the activity.