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 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.
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_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 |
Inherited constants |
|---|
android.widget.TextView
|
android.view.View
|
Inherited fields |
|---|
android.view.View
|
androidx.core.widget.AutoSizeableTextView
|
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 |
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. |
ColorStateList
|
getRippleColor()
Gets the ripple color for this button. |
ShapeAppearanceModel
|
getShapeAppearanceModel()
Returns the |
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()
|
void
|
onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent)
|
void
|
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
|
boolean
|
performClick()
|
void
|
removeOnCheckedChangeListener(MaterialButton.OnCheckedChangeListener listener)
Remove a listener that was previously added via |
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
|
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 |
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
|
toggle()
|
Protected methods | |
|---|---|
int[]
|
onCreateDrawableState(int extraSpace)
|
void
|
onLayout(boolean changed, int left, int top, int right, int bottom)
|
void
|
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
void
|
onTextChanged(CharSequence charSequence, int i, int i1, int i2)
|
Inherited methods | |
|---|---|
androidx.appcompat.widget.AppCompatButton
| |
android.widget.Button
| |
android.widget.TextView
| |
android.view.View
| |
java.lang.Object
| |
androidx.core.view.TintableBackgroundView
| |
androidx.core.widget.AutoSizeableTextView
| |
android.view.ViewTreeObserver.OnPreDrawListener
| |
android.graphics.drawable.Drawable.Callback
| |
android.view.KeyEvent.Callback
| |
android.view.accessibility.AccessibilityEventSource
| |
android.widget.Checkable
| |
com.google.android.material.shape.Shapeable
| |
XML attributes
MaterialButton_android_checkable
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
int ICON_GRAVITY_END
Gravity used to position the icon at the end of the view.
See also:
Constant Value: 3 (0x00000003)
ICON_GRAVITY_START
int ICON_GRAVITY_START
Gravity used to position the icon at the start of the view.
See also:
Constant Value: 1 (0x00000001)
ICON_GRAVITY_TEXT_END
int ICON_GRAVITY_TEXT_END
Gravity used to position the icon in the center of the view at the end of the text
See also:
Constant Value: 4 (0x00000004)
ICON_GRAVITY_TEXT_START
int ICON_GRAVITY_TEXT_START
Gravity used to position the icon in the center of the view at the start of the text
See also:
Constant Value: 2 (0x00000002)
Public constructors
MaterialButton
MaterialButton (Context context)
| Parameters | |
|---|---|
context |
Context |
MaterialButton
MaterialButton (Context context,
AttributeSet attrs)
| Parameters | |
|---|---|
context |
Context |
attrs |
AttributeSet |
MaterialButton
MaterialButton (Context context,
AttributeSet attrs,
int defStyleAttr)
| Parameters | |
|---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
Public methods
addOnCheckedChangeListener
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
void clearOnCheckedChangeListeners ()
Remove all previously added MaterialButton.OnCheckedChangeListeners.
getBackgroundTintList
ColorStateList getBackgroundTintList ()
| Returns | |
|---|---|
ColorStateList |
|
getBackgroundTintMode
PorterDuff.Mode getBackgroundTintMode ()
| Returns | |
|---|---|
PorterDuff.Mode |
|
getCornerRadius
int getCornerRadius ()
Gets the corner radius for this button.
Related XML Attributes:
| Returns | |
|---|---|
int |
Corner radius for this button. |
getIcon
Drawable getIcon ()
Gets the icon shown for this button, if present.
Related XML Attributes:
| Returns | |
|---|---|
Drawable |
Icon shown for this button, if present. |
See also:
getIconGravity
int getIconGravity ()
Gets the icon gravity for this button
Related XML Attributes:
| Returns | |
|---|---|
int |
Icon gravity of the button. |
See also:
getIconPadding
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. |
See also:
getIconSize
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
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
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. |
See also:
getRippleColor
ColorStateList getRippleColor ()
Gets the ripple color for this button.
Related XML Attributes:
| Returns | |
|---|---|
ColorStateList |
The color used for the ripple. |
getShapeAppearanceModel
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
ColorStateList getStrokeColor ()
Gets the stroke color for this button.
Related XML Attributes:
| Returns | |
|---|---|
ColorStateList |
The color used for the stroke. |
getStrokeWidth
int getStrokeWidth ()
Gets the stroke width for this button.
Related XML Attributes:
| Returns | |
|---|---|
int |
Stroke width for this button. |
isCheckable
boolean isCheckable ()
Returns whether this MaterialButton is checkable.
Related XML Attributes:
| Returns | |
|---|---|
boolean |
|
See also:
isChecked
boolean isChecked ()
| Returns | |
|---|---|
boolean |
|
onInitializeAccessibilityEvent
void onInitializeAccessibilityEvent (AccessibilityEvent accessibilityEvent)
| Parameters | |
|---|---|
accessibilityEvent |
AccessibilityEvent |
onInitializeAccessibilityNodeInfo
void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)
| Parameters | |
|---|---|
info |
AccessibilityNodeInfo |
performClick
boolean performClick ()
| Returns | |
|---|---|
boolean |
|
removeOnCheckedChangeListener
void removeOnCheckedChangeListener (MaterialButton.OnCheckedChangeListener listener)
Remove a listener that was previously added via addOnCheckedChangeListener(OnCheckedChangeListener).
| Parameters | |
|---|---|
listener |
MaterialButton.OnCheckedChangeListener: listener to remove
|
setBackground
void setBackground (Drawable background)
| Parameters | |
|---|---|
background |
Drawable |
setBackgroundColor
void setBackgroundColor (int color)
| Parameters | |
|---|---|
color |
int |
setBackgroundDrawable
void setBackgroundDrawable (Drawable background)
| Parameters | |
|---|---|
background |
Drawable |
setBackgroundResource
void setBackgroundResource (int backgroundResourceId)
| Parameters | |
|---|---|
backgroundResourceId |
int |
setBackgroundTintList
void setBackgroundTintList (ColorStateList tintList)
| Parameters | |
|---|---|
tintList |
ColorStateList |
setBackgroundTintMode
void setBackgroundTintMode (PorterDuff.Mode tintMode)
| Parameters | |
|---|---|
tintMode |
PorterDuff.Mode |
setCheckable
void setCheckable (boolean checkable)
Sets whether this MaterialButton is checkable.
Related XML Attributes:
| Parameters | |
|---|---|
checkable |
boolean: Whether this button is checkable. |
setChecked
void setChecked (boolean checked)
| Parameters | |
|---|---|
checked |
boolean |
setCornerRadius
void setCornerRadius (int cornerRadius)
Sets the corner radius for this button.
Related XML Attributes:
| Parameters | |
|---|---|
cornerRadius |
int: Corner radius for this button. |
setCornerRadiusResource
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. |
See also:
setElevation
void setElevation (float elevation)
| Parameters | |
|---|---|
elevation |
float |
setIcon
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. |
See also:
setIconGravity
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
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
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. |
See also:
setIconSize
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
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. |
See also:
setIconTintMode
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
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. |
See also:
setPressed
void setPressed (boolean pressed)
| Parameters | |
|---|---|
pressed |
boolean |
setRippleColor
void setRippleColor (ColorStateList rippleColor)
Sets the ripple color for this button.
Related XML Attributes:
| Parameters | |
|---|---|
rippleColor |
ColorStateList: Color to use for the ripple. |
setRippleColorResource
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
void setShapeAppearanceModel (ShapeAppearanceModel shapeAppearanceModel)
Sets the ShapeAppearanceModel that defines the component's MaterialShapeDrawable's shape using shapeAppearanceModel.
| Parameters | |
|---|---|
shapeAppearanceModel |
ShapeAppearanceModel |
| Throws | |
|---|---|
IllegalStateException |
if the MaterialButton's background has been overwritten. |
setStrokeColor
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
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
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
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. |
See also:
toggle
void toggle ()
Protected methods
onCreateDrawableState
int[] onCreateDrawableState (int extraSpace)
| Parameters | |
|---|---|
extraSpace |
int |
| Returns | |
|---|---|
int[] |
|
onLayout
void onLayout (boolean changed,
int left,
int top,
int right,
int bottom)
| Parameters | |
|---|---|
changed |
boolean |
left |
int |
top |
int |
right |
int |
bottom |
int |
onMeasure
void onMeasure (int widthMeasureSpec,
int heightMeasureSpec)
| Parameters | |
|---|---|
widthMeasureSpec |
int |
heightMeasureSpec |
int |
onTextChanged
void onTextChanged (CharSequence charSequence,
int i,
int i1,
int i2)
| Parameters | |
|---|---|
charSequence |
CharSequence |
i |
int |
i1 |
int |
i2 |
int |
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.

