MaterialCheckBox

public class MaterialCheckBox


A class that creates a Material Themed CheckBox.

This class uses attributes from the Material Theme to style a CheckBox. It behaves similarly to AppCompatCheckBox, but with color changes and the support of the indeterminate state, and an independent error state.

The checkbox is composed of an app:buttonCompat button drawable (the squared icon) and an app:buttonIcon icon drawable (the checkmark icon) layered on top of it. Their colors can be customized via app:buttonTint and app:buttonIconTint respectively.

If setting a custom app:buttonCompat, make sure to also set app:buttonIcon if an icon is desired. The checkbox does not support having a custom app:buttonCompat and preserving the default app:buttonIcon checkmark at the same time.

For more information, see the component developer guidance and design guidelines.

Summary

Nested types

Callback interface invoked when one of three independent checkbox states change.

Callback interface invoked when the checkbox error state changes.

Constants

static final int

The checked state of the checkbox.

static final int

The indeterminate state of the checkbox.

static final int

The unchecked state of the checkbox.

Public constructors

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

Public methods

void

Adds a OnCheckedStateChangedListener that will be invoked when the checkbox state changes.

void

Adds a OnErrorChangedListener that will be invoked when the checkbox error state changes.

void

Removes all previously added OnCheckedStateChangedListeners.

void

Remove all previously added OnErrorChangedListeners.

Drawable
Drawable

Returns the button icon drawable, or null if none.

ColorStateList

Returns the checkbox button icon's tint list.

Mode

Returns the blending mode used to apply the tint to the button icon drawable.

ColorStateList
int

Returns the current checkbox state.

CharSequence

Returns the accessibility label used for the error state announcement.

boolean

Returns true if this MaterialCheckBox will center the checkbox icon when there is no text.

boolean
boolean

Returns whether the checkbox is on error state.

boolean

Returns true if this MaterialCheckBox defaults to colors from a Material Theme.

void
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
void
onRestoreInstanceState(Parcelable state)
Parcelable
void

Removes a listener that was previously added via addOnCheckedStateChangedListener

void

Remove a listener that was previously added via addOnErrorChangedListener

void
setButtonDrawable(Drawable drawable)
void
setButtonDrawable(int resId)
void
setButtonIconDrawable(Drawable drawable)

Sets the button icon drawable of the checkbox.

void

Sets the button icon drawable of the checkbox.

void
setButtonIconTintList(ColorStateList tintList)

Sets the checkbox button icon's tint list, if an icon is present.

void
setButtonIconTintMode(Mode tintMode)

Specifies the blending mode used to apply the tint specified by setButtonIconTintList} to the button icon drawable.

void
setButtonTintList(ColorStateList tintList)
void
setButtonTintMode(Mode tintMode)
void
setCenterIfNoTextEnabled(boolean centerIfNoTextEnabled)

Sets whether this MaterialCheckBox should center the checkbox icon when there is no text.

void
setChecked(boolean checked)
void
setCheckedState(int checkedState)

Sets the CheckedState of the checkbox.

void
setErrorAccessibilityLabel(CharSequence errorAccessibilityLabel)

Sets the accessibility label to be used for the error state announcement by screen readers.

void

Sets the accessibility label to be used for the error state announcement by screen readers.

void
setErrorShown(boolean errorShown)

Sets whether the checkbox should be on error state.

void
setOnCheckedChangeListener(OnCheckedChangeListener listener)
void
void
setUseMaterialThemeColors(boolean useMaterialThemeColors)

Forces the MaterialCheckBox to use colors from a Material Theme.

void

Protected methods

void
int[]
onCreateDrawableState(int extraSpace)
void
onDraw(Canvas canvas)

Constants

STATE_CHECKED

public static final int STATE_CHECKED = 1

The checked state of the checkbox.

STATE_INDETERMINATE

public static final int STATE_INDETERMINATE = 2

The indeterminate state of the checkbox.

STATE_UNCHECKED

public static final int STATE_UNCHECKED = 0

The unchecked state of the checkbox. A checkbox is unchecked by default.

Public fields

buttonDrawable

public Drawable buttonDrawable

buttonIconDrawable

public Drawable buttonIconDrawable

buttonIconTintList

public ColorStateList buttonIconTintList

buttonIconTintMode

public Mode buttonIconTintMode

buttonTintList

public ColorStateList buttonTintList

checkedState

public int checkedState

errorAccessibilityLabel

public CharSequence errorAccessibilityLabel

Public constructors

MaterialCheckBox

public MaterialCheckBox(Context context)

MaterialCheckBox

public MaterialCheckBox(Context context, AttributeSet attrs)

MaterialCheckBox

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

Public methods

addOnCheckedStateChangedListener

public void addOnCheckedStateChangedListener(
    MaterialCheckBox.OnCheckedStateChangedListener listener
)

Adds a OnCheckedStateChangedListener that will be invoked when the checkbox state changes.

Components that add a listener should take care to remove it when finished via removeOnCheckedStateChangedListener.

Parameters
MaterialCheckBox.OnCheckedStateChangedListener listener

listener to add

addOnErrorChangedListener

public void addOnErrorChangedListener(
    MaterialCheckBox.OnErrorChangedListener listener
)

Adds a OnErrorChangedListener that will be invoked when the checkbox error state changes.

Components that add a listener should take care to remove it when finished via removeOnErrorChangedListener.

Parameters
MaterialCheckBox.OnErrorChangedListener listener

listener to add

clearOnCheckedStateChangedListeners

public void clearOnCheckedStateChangedListeners()

Removes all previously added OnCheckedStateChangedListeners.

clearOnErrorChangedListeners

public void clearOnErrorChangedListeners()

Remove all previously added OnErrorChangedListeners.

getButtonDrawable

public Drawable getButtonDrawable()

getButtonIconDrawable

public Drawable getButtonIconDrawable()

Returns the button icon drawable, or null if none.

This method expects that the icon will be the second layer of a two-layer drawable.

ref com.google.android.material.R.styleable#MaterialCheckBox_buttonIcon

getButtonIconTintList

public ColorStateList getButtonIconTintList()

Returns the checkbox button icon's tint list.

ref com.google.android.material.R.styleable#MaterialCheckBox_buttonIconTint

getButtonIconTintMode

public Mode getButtonIconTintMode()

Returns the blending mode used to apply the tint to the button icon drawable.

ref com.google.android.material.R.styleable#MaterialSwitch_buttonIconTintMode

getButtonTintList

public ColorStateList getButtonTintList()

getCheckedState

public int getCheckedState()

Returns the current checkbox state.

See also
setCheckedState

getErrorAccessibilityLabel

public CharSequence getErrorAccessibilityLabel()

Returns the accessibility label used for the error state announcement.

ref com.google.android.material.R.styleable#MaterialCheckBox_errorAccessibilityLabel

isCenterIfNoTextEnabled

public boolean isCenterIfNoTextEnabled()

Returns true if this MaterialCheckBox will center the checkbox icon when there is no text.

isChecked

public boolean isChecked()

isErrorShown

public boolean isErrorShown()

Returns whether the checkbox is on error state.

ref com.google.android.material.R.styleable#MaterialCheckBox_errorShown

See also
setErrorShown

isUseMaterialThemeColors

public boolean isUseMaterialThemeColors()

Returns true if this MaterialCheckBox defaults to colors from a Material Theme.

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)

onRestoreInstanceState

public void onRestoreInstanceState(Parcelable state)

onSaveInstanceState

public Parcelable onSaveInstanceState()

removeOnCheckedStateChangedListener

public void removeOnCheckedStateChangedListener(
    MaterialCheckBox.OnCheckedStateChangedListener listener
)

Removes a listener that was previously added via addOnCheckedStateChangedListener

Parameters
MaterialCheckBox.OnCheckedStateChangedListener listener

listener to remove

removeOnErrorChangedListener

public void removeOnErrorChangedListener(
    MaterialCheckBox.OnErrorChangedListener listener
)

Remove a listener that was previously added via addOnErrorChangedListener

Parameters
MaterialCheckBox.OnErrorChangedListener listener

listener to remove

setButtonDrawable

public void setButtonDrawable(Drawable drawable)

setButtonDrawable

public void setButtonDrawable(int resId)

setButtonIconDrawable

public void setButtonIconDrawable(Drawable drawable)

Sets the button icon drawable of the checkbox.

The icon will be layered above the button drawable set by setButtonDrawable.

ref com.google.android.material.R.styleable#MaterialCheckBox_buttonIcon

Parameters
Drawable drawable

the icon drawable to be set

setButtonIconDrawableResource

public void setButtonIconDrawableResource(int resId)

Sets the button icon drawable of the checkbox.

The icon will be layered above the button drawable set by setButtonDrawable.

ref com.google.android.material.R.styleable#MaterialCheckBox_buttonIcon

Parameters
int resId

resource id of the drawable to set, or 0 to clear and remove the icon

setButtonIconTintList

public void setButtonIconTintList(ColorStateList tintList)

Sets the checkbox button icon's tint list, if an icon is present.

This method expects that the icon will be the second layer of a two-layer drawable.

ref com.google.android.material.R.styleable#MaterialCheckBox_buttonIconTint

Parameters
ColorStateList tintList

the tint to set on the button icon

setButtonIconTintMode

public void setButtonIconTintMode(Mode tintMode)

Specifies the blending mode used to apply the tint specified by setButtonIconTintList} to the button icon drawable. The default mode is SRC_IN.

ref com.google.android.material.R.styleable#MaterialCheckBox_buttonIconTintMode

Parameters
Mode tintMode

the blending mode used to apply the tint

setButtonTintList

public void setButtonTintList(ColorStateList tintList)

setButtonTintMode

public void setButtonTintMode(Mode tintMode)

setCenterIfNoTextEnabled

public void setCenterIfNoTextEnabled(boolean centerIfNoTextEnabled)

Sets whether this MaterialCheckBox should center the checkbox icon when there is no text. Default is true.

setChecked

public void setChecked(boolean checked)

setCheckedState

public void setCheckedState(int checkedState)

Sets the CheckedState of the checkbox.

Parameters
int checkedState

the checked, unchecked, or indeterminate state to be set

See also
getCheckedState

setErrorAccessibilityLabel

public void setErrorAccessibilityLabel(CharSequence errorAccessibilityLabel)

Sets the accessibility label to be used for the error state announcement by screen readers.

ref com.google.android.material.R.styleable#MaterialCheckBox_errorAccessibilityLabel

Parameters
CharSequence errorAccessibilityLabel

the error announcement

setErrorAccessibilityLabelResource

public void setErrorAccessibilityLabelResource(int resId)

Sets the accessibility label to be used for the error state announcement by screen readers.

ref com.google.android.material.R.styleable#MaterialCheckBox_errorAccessibilityLabel

Parameters
int resId

resource ID of the error announcement text

setErrorShown

public void setErrorShown(boolean errorShown)

Sets whether the checkbox should be on error state. If true, the error color will be applied to the checkbox.

ref com.google.android.material.R.styleable#MaterialCheckBox_errorShown

Parameters
boolean errorShown

whether the checkbox should be on error state.

See also
isErrorShown

setOnCheckedChangeListener

public void setOnCheckedChangeListener(OnCheckedChangeListener listener)

setStateDescription

public void setStateDescription(CharSequence stateDescription)

setUseMaterialThemeColors

public void setUseMaterialThemeColors(boolean useMaterialThemeColors)

Forces the MaterialCheckBox to use colors from a Material Theme. Overrides any specified ButtonTintList. If set to false, sets the tints to null.

toggle

public void toggle()

Protected methods

onAttachedToWindow

protected void onAttachedToWindow()

onCreateDrawableState

protected int[] onCreateDrawableState(int extraSpace)

onDraw

protected void onDraw(Canvas canvas)