Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

MaterialButton

public class MaterialButton
extends AppCompatButton

java.lang.Object
   ↳ 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 ERROR(/com.google.android.material.button.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/colorAccent for the background tint color and white for the text color. For unfilled buttons, this class uses ?attr/colorAccent for the text color and transparent for the background tint.

Add icons to the start of this button using the ERROR(app:icon/R.attr#icon app:icon), ERROR(app:iconPadding/R.attr#iconPadding app:iconPadding), ERROR(app:iconTint/R.attr#iconTint app:iconTint) and ERROR(app:iconTintMode/R.attr#iconTintMode app:iconTintMode) attributes.

Add additional padding to the left and right side of the button icon, when present, using the ERROR(app:additionalPaddingLeftForIcon/R.attr#additionalPaddingLeftForIcon app:additionalPaddingLeftForIcon) and ERROR(app:additionalPaddingRightForIcon/R.attr#additionalPaddingRightForIcon app:additionalPaddingRightForIcon) attributes.

Specify background tint using the ERROR(app:backgroundTint/R.attr#backgroundTint app:backgroundTint) and ERROR(app:backgroundTintMode/R.attr#backgroundTintMode app:backgroundTintMode) attributes, which accepts either a color or a color state list.

Ripple color / press state color can be specified using the ERROR(app:rippleColor/R.attr#rippleColor 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 ERROR(app:strokeColor/R.attr#strokeColor app:strokeColor) attribute, which accepts either a color or a color state list. Stroke width can be set using the ERROR(app:strokeWidth/R.attr#strokeWidth app:strokeWidth) attribute.

Specify the radius of all four corners of the button using the ERROR(app:cornerRadius/R.attr#cornerRadius app:cornerRadius) attribute.

Summary

Public constructors

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

Public methods

void draw(Canvas canvas)
int getAdditionalPaddingLeftForIcon()

Get the additional padding added/removed to the left side of this button when an icon is present.

int getAdditionalPaddingRightForIcon()

Get the additional padding added/removed to the right side of this button when an icon is present.

ColorStateList getBackgroundTintList()
Mode getBackgroundTintMode()
int getButtonPaddingBottom()
int getButtonPaddingEnd()
int getButtonPaddingStart()
int getButtonPaddingTop()
int getCornerRadius()

Gets the corner radius for this button.

Drawable getIcon()

Gets the icon shown for this button, if present.

int getIconPadding()

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

ColorStateList getIconTint()

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

Mode getIconTintMode()

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

ColorStateList getRippleColor()

Gets the ripple color for this button.

ColorStateList getStrokeColor()

Gets the stroke color for this button.

int getStrokeWidth()

Gets the stroke width for this button.

void setAdditionalPaddingLeftForIcon(int additionalPaddingLeftForIcon)

Set the additional padding to add/remove to the left side of this button when an icon is present.

void setAdditionalPaddingRightForIcon(int additionalPaddingRightForIcon)

Set the additional padding to add/remove to the right side of this button when an icon is present.

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 setButtonPadding(int start, int top, int end, int bottom)

Sets the start, top, end, and bottom padding values for this button.

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 setIcon(Drawable icon)

Sets the icon to show 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 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 setRippleColor(ColorStateList rippleColor)

Sets the ripple color for this button.

void setRippleColorResource(int rippleColorResourceId)

Sets the ripple color resource for this button.

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.

Protected methods

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

Inherited methods

From class java.lang.Object

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

draw

void draw (Canvas canvas)

Parameters
canvas Canvas

getAdditionalPaddingLeftForIcon

int getAdditionalPaddingLeftForIcon ()

Get the additional padding added/removed to the left side of this button when an icon is present.

Returns
int Additional padding added/removed to the left side of this button when an icon is present

getAdditionalPaddingRightForIcon

int getAdditionalPaddingRightForIcon ()

Get the additional padding added/removed to the right side of this button when an icon is present.

Returns
int Additional padding added/removed to the right side of this button when an icon is present

getBackgroundTintList

ColorStateList getBackgroundTintList ()

Returns
ColorStateList

getBackgroundTintMode

Mode getBackgroundTintMode ()

Returns
Mode

getButtonPaddingBottom

int getButtonPaddingBottom ()

Returns
int Padding bottom value for this button.

getButtonPaddingEnd

int getButtonPaddingEnd ()

Returns
int Padding end value for this button.

getButtonPaddingStart

int getButtonPaddingStart ()

Returns
int Padding start value for this button.

getButtonPaddingTop

int getButtonPaddingTop ()

Returns
int Padding top value for this button.

getCornerRadius

int getCornerRadius ()

Gets the corner radius for this button.

Returns
int Corner radius for this button.

getIcon

Drawable getIcon ()

Gets the icon shown for this button, if present.

Returns
Drawable Icon shown for this button, if present.

getIconPadding

int getIconPadding ()

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

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

getIconTint

ColorStateList getIconTint ()

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

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

getIconTintMode

Mode getIconTintMode ()

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

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

getRippleColor

ColorStateList getRippleColor ()

Gets the ripple color for this button.

Returns
ColorStateList The color used for the ripple.

getStrokeColor

ColorStateList getStrokeColor ()

Gets the stroke color for this button.

Returns
ColorStateList The color used for the stroke.

getStrokeWidth

int getStrokeWidth ()

Gets the stroke width for this button.

Returns
int Stroke width for this button.

setAdditionalPaddingLeftForIcon

void setAdditionalPaddingLeftForIcon (int additionalPaddingLeftForIcon)

Set the additional padding to add/remove to the left side of this button when an icon is present.

Parameters
additionalPaddingLeftForIcon int: Additional padding to add/remove to the left side of this button when an icon is present

setAdditionalPaddingRightForIcon

void setAdditionalPaddingRightForIcon (int additionalPaddingRightForIcon)

Set the additional padding to add/remove to the right side of this button when an icon is present.

Parameters
additionalPaddingRightForIcon int: Additional padding to add/remove to the right side of this button when an icon is present

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 (Mode tintMode)

Parameters
tintMode Mode

setButtonPadding

void setButtonPadding (int start, 
                int top, 
                int end, 
                int bottom)

Sets the start, top, end, and bottom padding values for this button. Note that this is compounded with any values set for ERROR(/#additionalPaddingLeftForIcon), ERROR(/#additionalPaddingRightForIcon), and any insets to calculate the full padding for the view.

To retrieve just the base padding values, use getButtonPaddingStart(), getButtonPaddingTop(), getButtonPaddingEnd(), getButtonPaddingBottom()

Parameters
start int

top int

end int

bottom int

setCornerRadius

void setCornerRadius (int cornerRadius)

Sets the corner radius for this button.

Parameters
cornerRadius int: Corner radius for this button.

setCornerRadiusResource

void setCornerRadiusResource (int cornerRadiusResourceId)

Sets the corner radius dimension resource for this button.

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

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.

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

setIconPadding

void setIconPadding (int iconPadding)

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

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.

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

setIconTint

void setIconTint (ColorStateList iconTint)

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

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

setIconTintMode

void setIconTintMode (Mode iconTintMode)

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

Parameters
iconTintMode 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.

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

setRippleColor

void setRippleColor (ColorStateList rippleColor)

Sets the ripple color for this button.

Parameters
rippleColor ColorStateList: Color to use for the ripple.

setRippleColorResource

void setRippleColorResource (int rippleColorResourceId)

Sets the ripple color resource for this button.

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

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.

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.

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.

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.

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

Protected methods

onLayout

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

Parameters
changed boolean

left int

top int

right int

bottom int