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.

Inheritors

Constructors

Link copied to clipboard
constructor(@NonNull context: Context)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int)

Types

Link copied to clipboard
annotation class IconGravity
Positions the icon can be set to.
Link copied to clipboard
Interface definition for a callback to be invoked when the button checked state changes.

Properties

Link copied to clipboard
Link copied to clipboard
Gravity used to position the icon at the end of the view.
Link copied to clipboard
Gravity used to position the icon at the start of the view.
Link copied to clipboard
Gravity used to position the icon in the center of the view at the end of the text
Link copied to clipboard
Gravity used to position the icon in the center of the view at the start of the text
Link copied to clipboard
Gravity used to position the icon in the center of the view at the top of the text
Link copied to clipboard
Gravity used to position the icon at the top of the view.
Link copied to clipboard
open var iconGravity: Int
Link copied to clipboard
@get:Px
open var iconPadding: Int
Link copied to clipboard
@get:Px
open var iconSize: Int
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
Add a listener that will be invoked when the checked state of this MaterialButton changes.
Link copied to clipboard
Remove all previously added OnCheckedChangeListeners.
Link copied to clipboard
open fun getCornerRadius(): Int
Gets the corner radius for this button.
Link copied to clipboard
Gets the bottom inset for this button
Link copied to clipboard
Gets the top inset for this button
Link copied to clipboard
Gets the ripple color for this button.
Link copied to clipboard
Returns the ShapeAppearanceModel used for this MaterialButton's shape definition.
Link copied to clipboard
Gets the stroke color for this button.
Link copied to clipboard
open fun getStrokeWidth(): Int
Gets the stroke width for this button.
Link copied to clipboard
open fun isCheckable(): Boolean
Returns whether this MaterialButton is checkable.
Link copied to clipboard
open fun isChecked(): Boolean
Link copied to clipboard
Returns whether or not clicking the button will toggle the checked state.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun performClick(): Boolean
Link copied to clipboard
Link copied to clipboard
Remove a listener that was previously added via addOnCheckedChangeListener.
Link copied to clipboard
open fun setBackground(@NonNull background: Drawable)
Link copied to clipboard
open fun setBackgroundColor(@ColorInt color: Int)
Link copied to clipboard
open fun setBackgroundDrawable(@NonNull background: Drawable)
Link copied to clipboard
open fun setBackgroundResource(@DrawableRes backgroundResourceId: Int)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun setCheckable(checkable: Boolean)
Sets whether this MaterialButton is checkable.
Link copied to clipboard
open fun setChecked(checked: Boolean)
Link copied to clipboard
open fun setCornerRadius(@Px cornerRadius: Int)
Sets the corner radius for this button.
Link copied to clipboard
open fun setCornerRadiusResource(@DimenRes cornerRadiusResourceId: Int)
Sets the corner radius dimension resource for this button.
Link copied to clipboard
Link copied to clipboard
open fun setIconResource(@DrawableRes iconResourceId: Int)
Sets the icon drawable resource to show for this button.
Link copied to clipboard
open fun setIconTintResource(@ColorRes iconTintResourceId: Int)
Sets the tint list color resource for the icon shown for this button.
Link copied to clipboard
open fun setInsetBottom(@Dimension insetBottom: Int)
Sets the button bottom inset
Link copied to clipboard
open fun setInsetTop(@Dimension insetTop: Int)
Sets the button top inset
Link copied to clipboard
open fun setPressed(pressed: Boolean)
Link copied to clipboard
open fun setRippleColor(@Nullable rippleColor: ColorStateList)
Sets the ripple color for this button.
Link copied to clipboard
open fun setRippleColorResource(@ColorRes rippleColorResourceId: Int)
Sets the ripple color resource for this button.
Link copied to clipboard
open fun setShapeAppearanceModel(@NonNull shapeAppearanceModel: ShapeAppearanceModel)
Sets the that defines the shape.
Link copied to clipboard
open fun setStrokeColor(@Nullable strokeColor: ColorStateList)
Sets the stroke color for this button.
Link copied to clipboard
open fun setStrokeColorResource(@ColorRes strokeColorResourceId: Int)
Sets the stroke color resource for this button.
Link copied to clipboard
open fun setStrokeWidth(@Px strokeWidth: Int)
Sets the stroke width for this button.
Link copied to clipboard
open fun setStrokeWidthResource(@DimenRes strokeWidthResourceId: Int)
Sets the stroke width dimension resource for this button.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun setToggleCheckedStateOnClick(toggleCheckedStateOnClick: Boolean)
Sets whether or not to toggle the button checked state on click.
Link copied to clipboard
open fun toggle()