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

ImageView.ScaleType getLogoScaleType()

Returns scale type of logo's ImageView

Integer getNavigationIconTint()

Gets the tint color of the toolbar's navigation icon, or null if no tint color has been set.

boolean isLogoAdjustViewBounds()

Returns logo's ImageView adjustViewBounds

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 setLogoAdjustViewBounds(boolean logoAdjustViewBounds)

Sets ImageView.adjustViewBounds for logo's ImageView.

void setLogoScaleType(ImageView.ScaleType logoScaleType)

Sets ImageView.ScaleType for logo's ImageView.

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

getLogoScaleType

public ImageView.ScaleType getLogoScaleType ()

Returns scale type of logo's ImageView

Returns
ImageView.ScaleType

See also:

getNavigationIconTint

public Integer getNavigationIconTint ()

Gets the tint color of the toolbar's navigation icon, or null if no tint color has been set.

Returns
Integer

isLogoAdjustViewBounds

public boolean isLogoAdjustViewBounds ()

Returns logo's ImageView adjustViewBounds

Returns
boolean

See also:

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

setLogoAdjustViewBounds

public void setLogoAdjustViewBounds (boolean logoAdjustViewBounds)

Sets ImageView.adjustViewBounds for logo's ImageView.

Parameters
logoAdjustViewBounds boolean

setLogoScaleType

public void setLogoScaleType (ImageView.ScaleType logoScaleType)

Sets ImageView.ScaleType for logo's ImageView.

Parameters
logoScaleType ImageView.ScaleType

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