Android ToolBar

| Page Views: 292

A Toolbar is a generalization of action bars for use within application layouts.

Traditionally, an actionbar has always part of an Activity's opaque window decor controlled by the framework.

Toolbar on the other hand may be placed at any arbitrary level of nesting within a view hierarchy.

ToolBar API Definition

ToolBar derives from ViewGroup and was added in version 25.1.0.

public class Toolbar extends ViewGroup {..}

Here's the API definition of ToolBar:

   ↳    android.view.View
       ↳    android.view.ViewGroup

Which Features can be Displayed in a ToolBar?

Toolbar supports a more focused feature set than ActionBar.

You can display the following in a ToolBar:

  1. NavigationButton : 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 within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar.The navigation button is vertically aligned within the Toolbar's minimum height, if set.

  2. A branded logo image : This may extend to the height of the bar and can be arbitrarily wide.

  3. A title and subtitle : The title should be a signpost for the Toolbar's current position in the 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.

  4. One or more custom views : The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.

  5. An action menu : The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions. Action buttons are vertically aligned within the Toolbar's minimum height, if set.

How can we use ToolBar as an ActionBar?

All you need to use a ToolBar as an action bar is the setSupportActionBar() method defined in AppCompatActivity class.

setSupportActionBar()'s main role is to set a Toolbar to act as the ActionBar for an Activity's window.

This method was added in API level 25.1.0 when ToolBar was first introduced in android framework.

setSupportActionBar expects a ToolBar object:

void setSupportActionBar (Toolbar toolbar)

If you set a non-null value to the setSupportActionBar(), invoking the getActionBar() method will return an ActionBar object that can be used to control the given toolbar as if it were a traditional window decor action bar.

The toolbar's menu will be populated with the Activity's options menu and the navigation button will be wired through the standard home menu select action.

In order to use a Toolbar within the Activity's window content the application must not request the window feature FEATURE_SUPPORT_ACTION_BAR.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post