MaterialToolbar

public class MaterialToolbar
extends Toolbar

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ androidx.appcompat.widget.Toolbar
         ↳ com.google.android.material.appbar.MaterialToolbar


MaterialToolbar is a Toolbar that implements certain Material features, such as elevation overlays for Dark Themes and centered titles.

Regarding the Dark Theme elevation overlays, it's important to note that the Material AppBarLayout component also provides elevation overlay support, and operates under the assumption that the child Toolbar does not have a background. While a MaterialToolbar with a transparent background can be used within an AppBarLayout, in terms of elevation overlays its main value comes into play with the standalone Toolbar case, when using the Widget.MaterialComponents.Toolbar.Surface style with elevation.

To get started with the MaterialToolbar component, use com.google.android.material.appbar.MaterialToolbar in your layout XML instead of androidx.appcompat.widget.Toolbar or Toolbar. E.g.,:

 <com.google.android.material.appbar.MaterialToolbar
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>
 

Summary

Inherited constants

Inherited fields

Public constructors

MaterialToolbar(Context context)
MaterialToolbar(Context context, AttributeSet attrs)
MaterialToolbar(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

boolean isSubtitleCentered()

Returns whether the subtitle text corresponding to the Toolbar.setSubtitle(int) method should be centered horizontally within the toolbar.

boolean isTitleCentered()

Returns whether the title text corresponding to the Toolbar.setTitle(int) method should be centered horizontally within the toolbar.

void setElevation(float elevation)
void setNavigationIcon(Drawable drawable)
void setNavigationIconTint(int navigationIconTint)

Sets the color of the toolbar's navigation icon.

void setSubtitleCentered(boolean subtitleCentered)

Sets whether the subtitle text corresponding to the Toolbar.setSubtitle(int) method should be centered horizontally within the toolbar.

void setTitleCentered(boolean titleCentered)

Sets whether the title text corresponding to the Toolbar.setTitle(int) method should be centered horizontally within the toolbar.

Protected methods

void onAttachedToWindow()
void onLayout(boolean changed, int left, int top, int right, int bottom)

Inherited methods

Public constructors

MaterialToolbar

public MaterialToolbar (Context context)

Parameters
context Context

MaterialToolbar

public MaterialToolbar (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

MaterialToolbar

public MaterialToolbar (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

isSubtitleCentered

public boolean isSubtitleCentered ()

Returns whether the subtitle text corresponding to the Toolbar.setSubtitle(int) method should be centered horizontally within the toolbar.

Returns
boolean

isTitleCentered

public boolean isTitleCentered ()

Returns whether the title text corresponding to the Toolbar.setTitle(int) method should be centered horizontally within the toolbar.

Returns
boolean

setElevation

public void setElevation (float elevation)

Parameters
elevation float

setNavigationIcon

public void setNavigationIcon (Drawable drawable)

Parameters
drawable Drawable

setNavigationIconTint

public void setNavigationIconTint (int navigationIconTint)

Sets the color of the toolbar's navigation icon.

Parameters
navigationIconTint int

setSubtitleCentered

public void setSubtitleCentered (boolean subtitleCentered)

Sets whether the subtitle text corresponding to the Toolbar.setSubtitle(int) method should be centered horizontally within the toolbar.

Note: it is not recommended to use centered titles in conjunction with a nested custom view, as there may be positioning and overlap issues.

Parameters
subtitleCentered boolean

setTitleCentered

public void setTitleCentered (boolean titleCentered)

Sets whether the title text corresponding to the Toolbar.setTitle(int) method should be centered horizontally within the toolbar.

Note: it is not recommended to use centered titles in conjunction with a nested custom view, as there may be positioning and overlap issues.

Parameters
titleCentered boolean

Protected methods

onAttachedToWindow

protected void onAttachedToWindow ()

onLayout

protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int