Material Button Toggle Group
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.