MaterialToolbar

public class 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 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

Public fields

ScaleType
Integer

Public constructors

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

Public methods

void

Clears the tint list of the toolbar's navigation icon.

ScaleType

Returns scale type of logo's ImageView

Integer

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

boolean

Returns logo's ImageView adjustViewBounds

boolean

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

boolean

Returns whether the title text corresponding to the setTitle 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(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 setSubtitle method should be centered horizontally within the toolbar.

void
setTitleCentered(boolean titleCentered)

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

Protected methods

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

Public fields

logoScaleType

public ScaleType logoScaleType
public Integer navigationIconTint

Public constructors

MaterialToolbar

public MaterialToolbar(Context context)

MaterialToolbar

public MaterialToolbar(Context context, AttributeSet attrs)

MaterialToolbar

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

Public methods

clearNavigationIconTint

public void clearNavigationIconTint()

Clears the tint list of the toolbar's navigation icon. E.g., if the navigation icon is an XML based vector drawable, calling this method will clear the android:tint.

getLogoScaleType

public ScaleType getLogoScaleType()

Returns scale type of logo's ImageView

See also
setLogoScaleType

Default - null

getNavigationIconTint

public Integer getNavigationIconTint()

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

isLogoAdjustViewBounds

public boolean isLogoAdjustViewBounds()

Returns logo's ImageView adjustViewBounds

See also
setLogoAdjustViewBounds

Default - false

isSubtitleCentered

public boolean isSubtitleCentered()

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

isTitleCentered

public boolean isTitleCentered()

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

See also
setTitleCentered

setElevation

public void setElevation(float elevation)

setLogoAdjustViewBounds

public void setLogoAdjustViewBounds(boolean logoAdjustViewBounds)

Sets ImageView.adjustViewBounds for logo's ImageView.

setLogoScaleType

public void setLogoScaleType(ScaleType logoScaleType)

Sets ImageView.ScaleType for logo's ImageView.

setNavigationIcon

public void setNavigationIcon(Drawable drawable)

setNavigationIconTint

public void setNavigationIconTint(int navigationIconTint)

Sets the color of the toolbar's navigation icon.

setSubtitleCentered

public void setSubtitleCentered(boolean subtitleCentered)

Sets whether the subtitle text corresponding to the setSubtitle 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.

setTitleCentered

public void setTitleCentered(boolean titleCentered)

Sets whether the title text corresponding to the setTitle 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.

Protected methods

onAttachedToWindow

protected void onAttachedToWindow()

onLayout

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