MaterialButton

public class MaterialButton
extends AppCompatButton implements Checkable, Shapeable

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Button
         ↳ androidx.appcompat.widget.AppCompatButton
           ↳ com.google.android.material.button.MaterialButton


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 classes

@interface MaterialButton.IconGravity

Positions the icon can be set to. 

interface MaterialButton.OnCheckedChangeListener

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

XML attributes

MaterialButton_android_checkable  
MaterialButton_android_insetBottom  
MaterialButton_android_insetTop  
MaterialButton_cornerRadius  
MaterialButton_icon  
MaterialButton_iconGravity  
MaterialButton_iconPadding  
MaterialButton_iconSize  
MaterialButton_iconTint  
MaterialButton_iconTintMode  
MaterialButton_rippleColor  
MaterialButton_strokeColor  
MaterialButton_strokeWidth  

Constants

int ICON_GRAVITY_END

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

int ICON_GRAVITY_START

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

int ICON_GRAVITY_TEXT_END

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

int ICON_GRAVITY_TEXT_START

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

int ICON_GRAVITY_TEXT_TOP

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

int ICON_GRAVITY_TOP

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

Inherited constants

Inherited fields

Public constructors

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

Public methods

void addOnCheckedChangeListener(MaterialButton.OnCheckedChangeListener listener)

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

void clearOnCheckedChangeListeners()

Remove all previously added MaterialButton.OnCheckedChangeListeners.

ColorStateList getBackgroundTintList()
PorterDuff.Mode getBackgroundTintMode()
int getCornerRadius()

Gets the corner radius for this button.

Drawable getIcon()

Gets the icon shown for this button, if present.

int getIconGravity()

Gets the icon gravity for this button

int getIconPadding()

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

int getIconSize()

Returns the size of the icon if it was set.

ColorStateList getIconTint()

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

PorterDuff.Mode getIconTintMode()

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

int getInsetBottom()

Gets the bottom inset for this button

int getInsetTop()

Gets the top inset for this button

ColorStateList getRippleColor()

Gets the ripple color for this button.

ShapeAppearanceModel getShapeAppearanceModel()

Returns the ShapeAppearanceModel used for this MaterialButton's shape definition.

ColorStateList getStrokeColor()

Gets the stroke color for this button.

int getStrokeWidth()

Gets the stroke width for this button.

boolean isCheckable()

Returns whether this MaterialButton is checkable.

boolean isChecked()
boolean isToggleCheckedStateOnClick()

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 onSaveInstanceState()
boolean performClick()
void refreshDrawableState()
void removeOnCheckedChangeListener(MaterialButton.OnCheckedChangeListener listener)

Remove a listener that was previously added via addOnCheckedChangeListener(OnCheckedChangeListener).

void setBackground(Drawable background)
void setBackgroundColor(int color)
void setBackgroundDrawable(Drawable background)
void setBackgroundResource(int backgroundResourceId)
void setBackgroundTintList(ColorStateList tintList)
void setBackgroundTintMode(PorterDuff.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 setIconGravity(int iconGravity)

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(PorterDuff.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 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 setShapeAppearanceModel(ShapeAppearanceModel shapeAppearanceModel)

Sets the ShapeAppearanceModel that defines the shape.

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 toggle()

Protected methods

void onAttachedToWindow()
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)

Inherited methods

XML attributes

MaterialButton_android_checkable

Related methods:

MaterialButton_android_insetBottom

Related methods:

MaterialButton_android_insetTop

Related methods:

MaterialButton_cornerRadius

Related methods:

MaterialButton_icon

Related methods:

MaterialButton_iconGravity

Related methods:

MaterialButton_iconPadding

Related methods:

MaterialButton_iconSize

Related methods:

MaterialButton_iconTint

Related methods:

MaterialButton_iconTintMode

Related methods:

MaterialButton_rippleColor

Related methods:

MaterialButton_strokeColor

Related methods:

MaterialButton_strokeWidth

Related methods:

Constants

ICON_GRAVITY_END

public static final int ICON_GRAVITY_END

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

Constant Value: 3 (0x00000003)

ICON_GRAVITY_START

public static final int ICON_GRAVITY_START

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

Constant Value: 1 (0x00000001)

ICON_GRAVITY_TEXT_END

public static final int ICON_GRAVITY_TEXT_END

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

Constant Value: 4 (0x00000004)

ICON_GRAVITY_TEXT_START

public static final int ICON_GRAVITY_TEXT_START

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

Constant Value: 2 (0x00000002)

ICON_GRAVITY_TEXT_TOP

public static final int ICON_GRAVITY_TEXT_TOP

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

Constant Value: 32 (0x00000020)

ICON_GRAVITY_TOP

public static final int ICON_GRAVITY_TOP

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

Constant Value: 16 (0x00000010)

Public constructors

MaterialButton

public MaterialButton (Context context)

Parameters
context Context

MaterialButton

public MaterialButton (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

MaterialButton

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

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 MaterialButton.OnCheckedChangeListener.

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

Parameters
listener MaterialButton.OnCheckedChangeListener: listener to add

clearOnCheckedChangeListeners

public void clearOnCheckedChangeListeners ()

Remove all previously added MaterialButton.OnCheckedChangeListeners.

getBackgroundTintList

public ColorStateList getBackgroundTintList ()

Returns
ColorStateList

getBackgroundTintMode

public PorterDuff.Mode getBackgroundTintMode ()

Returns
PorterDuff.Mode

getCornerRadius

public int getCornerRadius ()

Gets the corner radius for this button.

Related XML Attributes:

Returns
int Corner radius for this button.

getIcon

public Drawable getIcon ()

Gets the icon shown for this button, if present.

Related XML Attributes:

Returns
Drawable Icon shown for this button, if present.

getIconGravity

public int getIconGravity ()

Gets the icon gravity for this button

Related XML Attributes:

Returns
int Icon gravity of the button.

getIconPadding

public int getIconPadding ()

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

Related XML Attributes:

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

getIconSize

public int getIconSize ()

Returns the size of the icon if it was set.

Related XML Attributes:

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

See also:

getIconTint

public ColorStateList getIconTint ()

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

Related XML Attributes:

Returns
ColorStateList Tint list for the icon shown for this button.

getIconTintMode

public PorterDuff.Mode getIconTintMode ()

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

Related XML Attributes:

Returns
PorterDuff.Mode Tint mode for the icon shown for this button.

getInsetBottom

public int getInsetBottom ()

Gets the bottom inset for this button

Related XML Attributes:

Returns
int

See also:

getInsetTop

public int getInsetTop ()

Gets the top inset for this button

Related XML Attributes:

Returns
int

See also:

getRippleColor

public ColorStateList getRippleColor ()

Gets the ripple color for this button.

Related XML Attributes:

Returns
ColorStateList The color used for the ripple.

getShapeAppearanceModel

public ShapeAppearanceModel getShapeAppearanceModel ()

Returns the ShapeAppearanceModel used for this MaterialButton's shape definition.

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

Returns
ShapeAppearanceModel

Throws
IllegalStateException if the MaterialButton's background has been overwritten.

getStrokeColor

public ColorStateList getStrokeColor ()

Gets the stroke color for this button.

Related XML Attributes:

Returns
ColorStateList The color used for the stroke.

getStrokeWidth

public int getStrokeWidth ()

Gets the stroke width for this button.

Related XML Attributes:

Returns
int Stroke width for this button.

isCheckable

public boolean isCheckable ()

Returns whether this MaterialButton is checkable.

Related XML Attributes:

Returns
boolean

isChecked

public boolean isChecked ()

Returns
boolean

isToggleCheckedStateOnClick

public boolean isToggleCheckedStateOnClick ()

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

Returns
boolean

onInitializeAccessibilityEvent

public void onInitializeAccessibilityEvent (AccessibilityEvent accessibilityEvent)

Parameters
accessibilityEvent AccessibilityEvent

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Parameters
info AccessibilityNodeInfo

onRestoreInstanceState

public void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

public Parcelable onSaveInstanceState ()

Returns
Parcelable

performClick

public boolean performClick ()

Returns
boolean

refreshDrawableState

public void refreshDrawableState ()

removeOnCheckedChangeListener

public void removeOnCheckedChangeListener (MaterialButton.OnCheckedChangeListener listener)

Remove a listener that was previously added via addOnCheckedChangeListener(OnCheckedChangeListener).

Parameters
listener MaterialButton.OnCheckedChangeListener: listener to remove

setBackground

public void setBackground (Drawable background)

Parameters
background Drawable

setBackgroundColor

public void setBackgroundColor (int color)

Parameters
color int

setBackgroundDrawable

public void setBackgroundDrawable (Drawable background)

Parameters
background Drawable

setBackgroundResource

public void setBackgroundResource (int backgroundResourceId)

Parameters
backgroundResourceId int

setBackgroundTintList

public void setBackgroundTintList (ColorStateList tintList)

Parameters
tintList ColorStateList

setBackgroundTintMode

public void setBackgroundTintMode (PorterDuff.Mode tintMode)

Parameters
tintMode PorterDuff.Mode

setCheckable

public void setCheckable (boolean checkable)

Sets whether this MaterialButton is checkable.

Related XML Attributes:

Parameters
checkable boolean: Whether this button is checkable.

setChecked

public void setChecked (boolean checked)

Parameters
checked boolean

setCornerRadius

public void setCornerRadius (int cornerRadius)

Sets the corner radius for this button.

Related XML Attributes:

Parameters
cornerRadius int: Corner radius for this button.

setCornerRadiusResource

public void setCornerRadiusResource (int cornerRadiusResourceId)

Sets the corner radius dimension resource for this button.

Related XML Attributes:

Parameters
cornerRadiusResourceId int: Corner radius dimension resource for this button.

setElevation

public void setElevation (float elevation)

Parameters
elevation float

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.

Related XML Attributes:

Parameters
icon Drawable: Drawable to use for the button's icon.

setIconGravity

public void setIconGravity (int iconGravity)

Sets the icon gravity for this button

Related XML Attributes:

Parameters
iconGravity int: icon gravity for this button

See also:

setIconPadding

public void setIconPadding (int iconPadding)

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

Related XML Attributes:

Parameters
iconPadding int: Padding between the button icon and the button text, if icon is present.

See also:

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.

Related XML Attributes:

Parameters
iconResourceId int: Drawable resource ID to use for the button's icon.

setIconSize

public void setIconSize (int iconSize)

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

Related XML Attributes:

Parameters
iconSize int: new dimension for width and height of the icon in pixels.

See also:

setIconTint

public void setIconTint (ColorStateList iconTint)

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

Related XML Attributes:

Parameters
iconTint ColorStateList: Tint list for the icon shown for this button.

setIconTintMode

public void setIconTintMode (PorterDuff.Mode iconTintMode)

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

Related XML Attributes:

Parameters
iconTintMode PorterDuff.Mode: Tint mode for the icon shown for this button.

See also:

setIconTintResource

public void setIconTintResource (int iconTintResourceId)

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

Related XML Attributes:

Parameters
iconTintResourceId int: Tint list color resource for the icon shown for this button.

setInsetBottom

public void setInsetBottom (int insetBottom)

Sets the button bottom inset

Related XML Attributes:

Parameters
insetBottom int

See also:

setInsetTop

public void setInsetTop (int insetTop)

Sets the button top inset

Related XML Attributes:

Parameters
insetTop int

See also:

setPressed

public void setPressed (boolean pressed)

Parameters
pressed boolean

setRippleColor

public void setRippleColor (ColorStateList rippleColor)

Sets the ripple color for this button.

Related XML Attributes:

Parameters
rippleColor ColorStateList: Color to use for the ripple.

setRippleColorResource

public void setRippleColorResource (int rippleColorResourceId)

Sets the ripple color resource for this button.

Related XML Attributes:

Parameters
rippleColorResourceId int: Color resource to use for the ripple.

setShapeAppearanceModel

public void setShapeAppearanceModel (ShapeAppearanceModel shapeAppearanceModel)

Sets the ShapeAppearanceModel that defines the shape.

Parameters
shapeAppearanceModel ShapeAppearanceModel

Throws
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.

Related XML Attributes:

Parameters
strokeColor ColorStateList: 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.

Related XML Attributes:

Parameters
strokeColorResourceId int: 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.

Related XML Attributes:

Parameters
strokeWidth int: 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.

Related XML Attributes:

Parameters
strokeWidthResourceId int: Stroke width dimension resource for this button.

setTextAlignment

public void setTextAlignment (int textAlignment)

Parameters
textAlignment int

setToggleCheckedStateOnClick

public void setToggleCheckedStateOnClick (boolean toggleCheckedStateOnClick)

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

Parameters
toggleCheckedStateOnClick boolean: whether or not to toggle the checked state on click.

toggle

public void toggle ()

Protected methods

onAttachedToWindow

protected void onAttachedToWindow ()

onCreateDrawableState

protected int[] onCreateDrawableState (int extraSpace)

Parameters
extraSpace int

Returns
int[]

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

onTextChanged

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

Parameters
charSequence CharSequence

i int

i1 int

i2 int