MaterialButtonToggleGroup

A common container for a set of related, toggleable MaterialButtons. The s in this group will be shown on a single line.

This layout currently only supports child views of type MaterialButton. Buttons can be added to this view group via XML, as follows:

<com.google.android.material.button.MaterialButtonToggleGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toggle_button_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.button.MaterialButton
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_private"/>
    <com.google.android.material.button.MaterialButton
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_team"/>
    <com.google.android.material.button.MaterialButton
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_everyone"/>
    <com.google.android.material.button.MaterialButton
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_custom"/>

</com.google.android.material.button.MaterialButtonToggleGroup>

Buttons can also be added to this view group programmatically via the addView methods.

Note: Styling must applied to each child button individually. It is recommended to use the materialButtonOutlinedStyle attribute for all child buttons. materialButtonOutlinedStyle will most closely match the Material Design guidelines for this component, and supports the checked state for child buttons.

Any MaterialButtons added to this view group are automatically marked as checkable, and by default multiple buttons within the same group can be checked. To enforce that only one button can be checked at a time, set the app:singleSelection attribute to true on the MaterialButtonToggleGroup or call setSingleSelection(true).

MaterialButtonToggleGroup is a LinearLayout. Using android:layout_width="MATCH_PARENT" and removing android:insetBottom android:insetTop on the children is recommended if using VERTICAL.

In order to cohesively group multiple buttons together, MaterialButtonToggleGroup overrides the start and end margins of any children added to this layout such that child buttons are placed directly adjacent to one another.

MaterialButtonToggleGroup also overrides any shapeAppearance, shapeAppearanceOverlay, or cornerRadius attribute set on MaterialButton children such that only the left-most corners of the first child and the right-most corners of the last child retain their shape appearance or corner size.

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

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
Interface definition for a callback to be invoked when a MaterialButton is checked or unchecked in this group.

Functions

Link copied to clipboard
Add a listener that will be invoked when the check state of a MaterialButton in this group changes.
Link copied to clipboard
open fun addView(child: View, index: Int, params: ViewGroup.LayoutParams)
This override prohibits Views other than MaterialButton to be added.
Link copied to clipboard
open fun check(@IdRes id: Int)
Sets the MaterialButton whose id is passed in to the checked state.
Link copied to clipboard
open fun clearChecked()
Clears the selections.
Link copied to clipboard
Remove all previously added OnButtonCheckedListeners.
Link copied to clipboard
When in single selection mode, returns the identifier of the selected button in this group.
Link copied to clipboard
Returns the identifiers of the selected MaterialButtons in this group.
Link copied to clipboard
Returns whether we prevent all child buttons from being deselected.
Link copied to clipboard
Returns whether this group only allows a single button to be checked.
Link copied to clipboard
open fun onViewRemoved(child: View)
Link copied to clipboard
Link copied to clipboard
open fun setEnabled(enabled: Boolean)
Enables this MaterialButtonToggleGroup and all its MaterialButton children
Link copied to clipboard
open fun setSelectionRequired(selectionRequired: Boolean)
Sets whether we prevent all child buttons from being deselected.
Link copied to clipboard
open fun setSingleSelection(singleSelection: Boolean)
Sets whether this group only allows a single button to be checked.
Link copied to clipboard
open fun uncheck(@IdRes id: Int)
Sets the MaterialButton whose id is passed in to the unchecked state.