A Toolbar
is a generalization of ActionBar for use within application layouts. While an ActionBar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar
may be placed at any arbitrary level of nesting within a view hierarchy. It can be added by performing the following steps:
Make sure the following dependency is added to your module's (e.g. app's) build.gradle file under dependencies:
Set the theme for your app
to one that does not have an ActionBar. To do that, edit your styles.xml
file under res/values, and set a Theme.AppCompat theme.
In this example we are using Theme.AppCompat.NoActionBar as parent of your AppTheme:
You can also use Theme.AppCompat.Light.NoActionBar
or Theme.AppCompat.DayNight.NoActionBar, or any other theme that does not inherently have an ActionBar
Add the Toolbar to your activity layout:
Below the Toolbar you can add the rest of your layout.
In your Activity, set the Toolbar as the ActionBar for this Activity. Provided that you're using the appcompat library and an AppCompatActivity, you would use the setSupportActionBar() method:
After performing the above steps, you can use the getSupportActionBar() method to manipulate the Toolbar that is set as the ActionBar.
For example, you can set the title as shown below:
For example, you can also set title and background
color as shown below:
adding a toolbar in androidandroid toolbar menutoolbar android material designandroid actionbar vs toolbaradd menu to toolbar androidandroid custom toolbar layoutandroid action bar iconsaction bar android studiocustom actionbar in android exampl