MaterialButton

public class MaterialButton implements Shapeable

Known direct subclasses
ExtendedFloatingActionButton

Extended floating action buttons are used for a special type of promoted action.


A convenience class for creating a new Material button.

This class supplies updated Material styles for the button in the constructor. The widget will display the correct default Material styles without the use of the style flag.

All attributes from com.google.android.material.R.styleable#MaterialButton are supported. Do not use the android:background attribute. MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.

For filled buttons, this class uses your theme's ?attr/colorPrimary for the background tint color and ?attr/colorOnPrimary for the text color. For unfilled buttons, this class uses ?attr/colorPrimary for the text color and transparent for the background tint.

Add icons to the start, center, or end of this button using the app:icon, app:iconPadding, app:iconTint, app:iconTintMode and app:iconGravity attributes.

If a start-aligned icon is added to this button, please use a style like one of the ".Icon" styles specified in the default MaterialButton styles. The ".Icon" styles adjust padding slightly to achieve a better visual balance. This style should only be used with a start-aligned icon button. If your icon is end-aligned, you cannot use a ".Icon" style and must instead manually adjust your padding such that the visual adjustment is mirrored.

Specify background tint using the app:backgroundTint and app:backgroundTintMode attributes, which accepts either a color or a color state list.

Ripple color / press state color can be specified using the app:rippleColor attribute. Ripple opacity will be determined by the Android framework when available. Otherwise, this color will be overlaid on the button at a 50% opacity when button is pressed.

Set the stroke color using the app:strokeColor attribute, which accepts either a color or a color state list. Stroke width can be set using the app:strokeWidth attribute.

Specify the radius of all four corners of the button using the app:cornerRadius attribute.

For more information, see the component developer guidance and design guidelines.

Summary

Nested types

@Retention(value = RetentionPolicy.SOURCE)
public annotation MaterialButton.IconGravity

Positions the icon can be set to.

Interface definition for a callback to be invoked when the button checked state changes.

Constants

static final int

Gravity used to position the icon at the end of the view.

static final int

Gravity used to position the icon at the start of the view.

static final int

Gravity used to position the icon in the center of the view at the end of the text

static final int

Gravity used to position the icon in the center of the view at the start of the text

static final int

Gravity used to position the icon in the center of the view at the top of the text

static final int

Gravity used to position the icon at the top of the view.

Public fields

Drawable
int
int
int
ColorStateList
Mode

Public constructors

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

Public methods

void

Add a listener that will be invoked when the checked state of this MaterialButton changes.

void

Remove all previously added OnCheckedChangeListeners.

ColorStateList
Mode
int

Gets the corner radius for this button.

Drawable

Gets the icon shown for this button, if present.

int

Gets the icon gravity for this button

int

Gets the padding between the button icon and the button text, if icon is present.

int

Returns the size of the icon if it was set.

ColorStateList

Gets the tint list for the icon shown for this button.

Mode

Gets the tint mode for the icon shown for this button.

int

Gets the bottom inset for this button

int

Gets the top inset for this button

ColorStateList

Gets the ripple color for this button.

ShapeAppearanceModel

Returns the ShapeAppearanceModel used for this MaterialButton's original drawables.

ColorStateList

Gets the stroke color for this button.

int

Gets the stroke width for this button.

boolean

Returns whether this MaterialButton is checkable.

boolean
boolean

Returns whether the optical center feature is enabled.

boolean

Returns whether or not clicking the button will toggle the checked state.

void
onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent)
void
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void
onRestoreInstanceState(Parcelable state)
Parcelable
boolean
void
void

Remove a listener that was previously added via addOnCheckedChangeListener.

void
void
setBackground(Drawable background)
void
setBackgroundColor(int color)
void
setBackgroundDrawable(Drawable background)
void
setBackgroundResource(int backgroundResourceId)
void
setBackgroundTintList(ColorStateList tintList)
void
setBackgroundTintMode(Mode tintMode)
void
setCheckable(boolean checkable)

Sets whether this MaterialButton is checkable.

void
setChecked(boolean checked)
void
setCornerRadius(int cornerRadius)

Sets the corner radius for this button.

void
setCornerRadiusResource(int cornerRadiusResourceId)

Sets the corner radius dimension resource for this button.

void
setElevation(float elevation)
void
setIcon(Drawable icon)

Sets the icon to show for this button.

void

Sets the icon gravity for this button

void
setIconPadding(int iconPadding)

Sets the padding between the button icon and the button text, if icon is present.

void
setIconResource(int iconResourceId)

Sets the icon drawable resource to show for this button.

void
setIconSize(int iconSize)

Sets the width and height of the icon.

void
setIconTint(ColorStateList iconTint)

Sets the tint list for the icon shown for this button.

void
setIconTintMode(Mode iconTintMode)

Sets the tint mode for the icon shown for this button.

void
setIconTintResource(int iconTintResourceId)

Sets the tint list color resource for the icon shown for this button.

void
setInsetBottom(int insetBottom)

Sets the button bottom inset

void
setInsetTop(int insetTop)

Sets the button top inset

void
setOpticalCenterEnabled(boolean opticalCenterEnabled)

Sets whether to enable the optical center feature.

void
setPressed(boolean pressed)
void
setRippleColor(ColorStateList rippleColor)

Sets the ripple color for this button.

void
setRippleColorResource(int rippleColorResourceId)

Sets the ripple color resource for this button.

void

Sets the ShapeAppearanceModel used for this MaterialButton's original drawables.

void
setStrokeColor(ColorStateList strokeColor)

Sets the stroke color for this button.

void
setStrokeColorResource(int strokeColorResourceId)

Sets the stroke color resource for this button.

void
setStrokeWidth(int strokeWidth)

Sets the stroke width for this button.

void
setStrokeWidthResource(int strokeWidthResourceId)

Sets the stroke width dimension resource for this button.

void
setTextAlignment(int textAlignment)
void
setToggleCheckedStateOnClick(boolean toggleCheckedStateOnClick)

Sets whether or not to toggle the button checked state on click.

void
setWidth(int pixels)
void

Protected methods

void
int[]
onCreateDrawableState(int extraSpace)
void
onLayout(boolean changed, int left, int top, int right, int bottom)
void
onTextChanged(CharSequence charSequence, int i, int i1, int i2)

Constants

ICON_GRAVITY_END

public static final int ICON_GRAVITY_END = 3

Gravity used to position the icon at the end of the view.

ICON_GRAVITY_START

public static final int ICON_GRAVITY_START = 1

Gravity used to position the icon at the start of the view.

ICON_GRAVITY_TEXT_END

public static final int ICON_GRAVITY_TEXT_END = 4

Gravity used to position the icon in the center of the view at the end of the text

ICON_GRAVITY_TEXT_START

public static final int ICON_GRAVITY_TEXT_START = 2

Gravity used to position the icon in the center of the view at the start of the text

ICON_GRAVITY_TEXT_TOP

public static final int ICON_GRAVITY_TEXT_TOP = 32

Gravity used to position the icon in the center of the view at the top of the text

ICON_GRAVITY_TOP

public static final int ICON_GRAVITY_TOP = 16

Gravity used to position the icon at the top of the view.

Public fields

icon

public Drawable icon

iconGravity

@MaterialButton.IconGravity
public int iconGravity

iconPadding

public int iconPadding

iconSize

public int iconSize

iconTint

public ColorStateList iconTint

iconTintMode

public Mode iconTintMode

Public constructors

MaterialButton

public MaterialButton(Context context)

MaterialButton

public MaterialButton(Context context, AttributeSet attrs)

MaterialButton

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

Public methods

addOnCheckedChangeListener

public void addOnCheckedChangeListener(
    MaterialButton.OnCheckedChangeListener listener
)

Add a listener that will be invoked when the checked state of this MaterialButton changes. See OnCheckedChangeListener.

Components that add a listener should take care to remove it when finished via removeOnCheckedChangeListener.

Parameters
MaterialButton.OnCheckedChangeListener listener

listener to add

clearOnCheckedChangeListeners

public void clearOnCheckedChangeListeners()

Remove all previously added OnCheckedChangeListeners.

getBackgroundTintList

public ColorStateList getBackgroundTintList()

getBackgroundTintMode

public Mode getBackgroundTintMode()

getCornerRadius

public int getCornerRadius()

Gets the corner radius for this button.

ref com.google.android.material.R.styleable#MaterialButton_cornerRadius

Returns
int

Corner radius for this button.

getIcon

public Drawable getIcon()

Gets the icon shown for this button, if present.

ref com.google.android.material.R.styleable#MaterialButton_icon

Returns
Drawable

Icon shown for this button, if present.

getIconGravity

@MaterialButton.IconGravity
public int getIconGravity()

Gets the icon gravity for this button

ref com.google.android.material.R.styleable#MaterialButton_iconGravity

Returns
int

Icon gravity of the button.

See also
setIconGravity

getIconPadding

public int getIconPadding()

Gets the padding between the button icon and the button text, if icon is present.

ref com.google.android.material.R.styleable#MaterialButton_iconPadding

Returns
int

Padding between the button icon and the button text, if icon is present.

See also
setIconPadding

getIconSize

public int getIconSize()

Returns the size of the icon if it was set.

ref com.google.android.material.R.styleable#MaterialButton_iconSize

Returns
int

Returns the size of the icon if it was set in pixels, 0 otherwise.

See also
setIconSize

getIconTint

public ColorStateList getIconTint()

Gets the tint list for the icon shown for this button.

ref com.google.android.material.R.styleable#MaterialButton_iconTint

Returns
ColorStateList

Tint list for the icon shown for this button.

getIconTintMode

public Mode getIconTintMode()

Gets the tint mode for the icon shown for this button.

ref com.google.android.material.R.styleable#MaterialButton_iconTintMode

Returns
Mode

Tint mode for the icon shown for this button.

See also
setIconTintMode

getInsetBottom

public int getInsetBottom()

Gets the bottom inset for this button

ref com.google.android.material.R.styleable#MaterialButton_android_insetBottom

See also
setInsetTop

getInsetTop

public int getInsetTop()

Gets the top inset for this button

ref com.google.android.material.R.styleable#MaterialButton_android_insetTop

See also
setInsetTop

getRippleColor

public ColorStateList getRippleColor()

Gets the ripple color for this button.

ref com.google.android.material.R.styleable#MaterialButton_rippleColor

Returns
ColorStateList

The color used for the ripple.

getShapeAppearanceModel

public ShapeAppearanceModel getShapeAppearanceModel()

Returns the ShapeAppearanceModel used for this MaterialButton's original drawables.

This ShapeAppearanceModel can be modified to change the component's shape.

Throws
java.lang.IllegalStateException

if the MaterialButton's background has been overwritten.

getStrokeColor

public ColorStateList getStrokeColor()

Gets the stroke color for this button.

ref com.google.android.material.R.styleable#MaterialButton_strokeColor

Returns
ColorStateList

The color used for the stroke.

getStrokeWidth

public int getStrokeWidth()

Gets the stroke width for this button.

ref com.google.android.material.R.styleable#MaterialButton_strokeWidth

Returns
int

Stroke width for this button.

isCheckable

public boolean isCheckable()

Returns whether this MaterialButton is checkable.

ref com.google.android.material.R.styleable#MaterialButton_android_checkable

See also
setCheckable

isChecked

public boolean isChecked()

isOpticalCenterEnabled

public boolean isOpticalCenterEnabled()

Returns whether the optical center feature is enabled.

isToggleCheckedStateOnClick

public boolean isToggleCheckedStateOnClick()

Returns whether or not clicking the button will toggle the checked state.

ref R.styleable#toggleCheckedStateOnClick

onInitializeAccessibilityEvent

public void onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent)

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)

onRestoreInstanceState

public void onRestoreInstanceState(Parcelable state)

onSaveInstanceState

public Parcelable onSaveInstanceState()

performClick

public boolean performClick()

refreshDrawableState

public void refreshDrawableState()

removeOnCheckedChangeListener

public void removeOnCheckedChangeListener(
    MaterialButton.OnCheckedChangeListener listener
)

Remove a listener that was previously added via addOnCheckedChangeListener.

Parameters
MaterialButton.OnCheckedChangeListener listener

listener to remove

setA11yClassName

public void setA11yClassName(String className)

setBackground

public void setBackground(Drawable background)

setBackgroundColor

public void setBackgroundColor(int color)

setBackgroundDrawable

public void setBackgroundDrawable(Drawable background)

setBackgroundResource

public void setBackgroundResource(int backgroundResourceId)

setBackgroundTintList

public void setBackgroundTintList(ColorStateList tintList)

setBackgroundTintMode

public void setBackgroundTintMode(Mode tintMode)

setCheckable

public void setCheckable(boolean checkable)

Sets whether this MaterialButton is checkable.

ref com.google.android.material.R.styleable#MaterialButton_android_checkable

Parameters
boolean checkable

Whether this button is checkable.

setChecked

public void setChecked(boolean checked)

setCornerRadius

public void setCornerRadius(int cornerRadius)

Sets the corner radius for this button.

ref com.google.android.material.R.styleable#MaterialButton_cornerRadius

Parameters
int cornerRadius

Corner radius for this button.

setCornerRadiusResource

public void setCornerRadiusResource(int cornerRadiusResourceId)

Sets the corner radius dimension resource for this button.

ref com.google.android.material.R.styleable#MaterialButton_cornerRadius

Parameters
int cornerRadiusResourceId

Corner radius dimension resource for this button.

setElevation

public void setElevation(float elevation)

setIcon

public void setIcon(Drawable icon)

Sets the icon to show for this button. By default, this icon will be shown on the left side of the button.

ref com.google.android.material.R.styleable#MaterialButton_icon

Parameters
Drawable icon

Drawable to use for the button's icon.

setIconGravity

public void setIconGravity(@MaterialButton.IconGravity int iconGravity)

Sets the icon gravity for this button

ref com.google.android.material.R.styleable#MaterialButton_iconGravity

Parameters
@MaterialButton.IconGravity int iconGravity

icon gravity for this button

See also
getIconGravity

setIconPadding

public void setIconPadding(int iconPadding)

Sets the padding between the button icon and the button text, if icon is present.

ref com.google.android.material.R.styleable#MaterialButton_iconPadding

Parameters
int iconPadding

Padding between the button icon and the button text, if icon is present.

See also
getIconPadding

setIconResource

public void setIconResource(int iconResourceId)

Sets the icon drawable resource to show for this button. By default, this icon will be shown on the left side of the button.

ref com.google.android.material.R.styleable#MaterialButton_icon

Parameters
int iconResourceId

Drawable resource ID to use for the button's icon.

See also
setIcon
getIcon

setIconSize

public void setIconSize(int iconSize)

Sets the width and height of the icon. Use 0 to use source Drawable size.

ref com.google.android.material.R.styleable#MaterialButton_iconSize

Parameters
int iconSize

new dimension for width and height of the icon in pixels.

See also
getIconSize

setIconTint

public void setIconTint(ColorStateList iconTint)

Sets the tint list for the icon shown for this button.

ref com.google.android.material.R.styleable#MaterialButton_iconTint

Parameters
ColorStateList iconTint

Tint list for the icon shown for this button.

setIconTintMode

public void setIconTintMode(Mode iconTintMode)

Sets the tint mode for the icon shown for this button.

ref com.google.android.material.R.styleable#MaterialButton_iconTintMode

Parameters
Mode iconTintMode

Tint mode for the icon shown for this button.

See also
getIconTintMode

setIconTintResource

public void setIconTintResource(int iconTintResourceId)

Sets the tint list color resource for the icon shown for this button.

ref com.google.android.material.R.styleable#MaterialButton_iconTint

Parameters
int iconTintResourceId

Tint list color resource for the icon shown for this button.

setInsetBottom

public void setInsetBottom(int insetBottom)

Sets the button bottom inset

ref com.google.android.material.R.styleable#MaterialButton_android_insetBottom

See also
getInsetBottom

setInsetTop

public void setInsetTop(int insetTop)

Sets the button top inset

ref com.google.android.material.R.styleable#MaterialButton_android_insetTop

See also
getInsetBottom

setOpticalCenterEnabled

public void setOpticalCenterEnabled(boolean opticalCenterEnabled)

Sets whether to enable the optical center feature.

Parameters
boolean opticalCenterEnabled

whether to enable optical centering.

setPressed

public void setPressed(boolean pressed)

setRippleColor

public void setRippleColor(ColorStateList rippleColor)

Sets the ripple color for this button.

ref com.google.android.material.R.styleable#MaterialButton_rippleColor

Parameters
ColorStateList rippleColor

Color to use for the ripple.

setRippleColorResource

public void setRippleColorResource(int rippleColorResourceId)

Sets the ripple color resource for this button.

ref com.google.android.material.R.styleable#MaterialButton_rippleColor

Parameters
int rippleColorResourceId

Color resource to use for the ripple.

setShapeAppearanceModel

public void setShapeAppearanceModel(ShapeAppearanceModel shapeAppearanceModel)

Sets the ShapeAppearanceModel used for this MaterialButton's original drawables.

Throws
java.lang.IllegalStateException

if the MaterialButton's background has been overwritten.

setStrokeColor

public void setStrokeColor(ColorStateList strokeColor)

Sets the stroke color for this button. Both stroke color and stroke width must be set for a stroke to be drawn.

ref com.google.android.material.R.styleable#MaterialButton_strokeColor

Parameters
ColorStateList strokeColor

Color to use for the stroke.

setStrokeColorResource

public void setStrokeColorResource(int strokeColorResourceId)

Sets the stroke color resource for this button. Both stroke color and stroke width must be set for a stroke to be drawn.

ref com.google.android.material.R.styleable#MaterialButton_strokeColor

Parameters
int strokeColorResourceId

Color resource to use for the stroke.

setStrokeWidth

public void setStrokeWidth(int strokeWidth)

Sets the stroke width for this button. Both stroke color and stroke width must be set for a stroke to be drawn.

ref com.google.android.material.R.styleable#MaterialButton_strokeWidth

Parameters
int strokeWidth

Stroke width for this button.

setStrokeWidthResource

public void setStrokeWidthResource(int strokeWidthResourceId)

Sets the stroke width dimension resource for this button. Both stroke color and stroke width must be set for a stroke to be drawn.

ref com.google.android.material.R.styleable#MaterialButton_strokeWidth

Parameters
int strokeWidthResourceId

Stroke width dimension resource for this button.

setTextAlignment

public void setTextAlignment(int textAlignment)

setToggleCheckedStateOnClick

public void setToggleCheckedStateOnClick(boolean toggleCheckedStateOnClick)

Sets whether or not to toggle the button checked state on click.

ref R.styleable#toggleCheckedStateOnClick

Parameters
boolean toggleCheckedStateOnClick

whether or not to toggle the checked state on click.

setWidth

public void setWidth(int pixels)

toggle

public void toggle()

Protected methods

onAttachedToWindow

protected void onAttachedToWindow()

onCreateDrawableState

protected int[] onCreateDrawableState(int extraSpace)

onLayout

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

onTextChanged

protected void onTextChanged(CharSequence charSequence, int i, int i1, int i2)