Join us on the livestream at Android Dev Summit on 7-8 November 2018, starting at 10AM PDT!

MaterialButton

public class MaterialButton
extends AppCompatButton

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Button
         ↳ android.support.v7.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/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 or center of this button of this button using the app:icon, app:iconPadding, app:iconTint, app:iconTintMode and app:iconGravity attributes.

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. 

XML attributes

MaterialButton_cornerRadius  
MaterialButton_icon  
MaterialButton_iconGravity  
MaterialButton_iconPadding  
MaterialButton_iconSize  
MaterialButton_iconTint  
MaterialButton_iconTintMode  
MaterialButton_rippleColor  
MaterialButton_strokeColor  
MaterialButton_strokeWidth  

Constants

int ICON_GRAVITY_START

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

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

From class android.widget.TextView
From class android.view.View

Inherited fields

From class android.view.View
From interface android.support.v4.widget.AutoSizeableTextView

Public constructors

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

Public methods

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.

ColorStateList getStrokeColor()

Gets the stroke color for this button.

int getStrokeWidth()

Gets the stroke width for this button.

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 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 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 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 onDraw(Canvas canvas)
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited methods

From class android.support.v7.widget.AppCompatButton
From class android.widget.Button
From class android.widget.TextView
From class android.view.View
From class java.lang.Object
From interface android.support.v4.view.TintableBackgroundView
From interface android.support.v4.widget.AutoSizeableTextView
From interface android.view.ViewTreeObserver.OnPreDrawListener
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML attributes

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_START

int ICON_GRAVITY_START

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

Constant Value: 1 (0x00000001)

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

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

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.

getIconGravity

int getIconGravity ()

Gets the icon gravity for this button

Related XML Attributes:

Returns
int Icon gravity of the button.

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.

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.

getRippleColor

ColorStateList getRippleColor ()

Gets the ripple color for this button.

Related XML Attributes:

Returns
ColorStateList The color used for the ripple.

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.

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

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.

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.

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.

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.

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.

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.

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.

Protected methods

onDraw

void onDraw (Canvas canvas)

Parameters
canvas Canvas

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