Toggle buttons can be used to select from a group of choices.
There are two types of toggle buttons:
Toggle button
To emphasize groups of related toggle buttons, a group should share a common container.
Toggle button examples
API and source code:
- MaterialButtonToggleGroup
- MaterialButton
The following example shows a toggle button with three buttons that have text labels.
In the layout:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
style="?attr/materialButtonOutlinedStyle"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
mplementing an icon-only toggle button
The following example shows a toggle button with three buttons that have icons.
In res/values/styles.xml:
<style name="Widget.App.Button.OutlinedButton.IconOnly" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="iconPadding">0dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:paddingLeft">12dp</item>
<item name="android:paddingRight">12dp</item>
<item name="android:minWidth">48dp</item>
<item name="android:minHeight">48dp</item>
</style>
In the layout:
<com.google.android.material.button.MaterialButtonToggleGroup
...>
<Button
...
app:icon="@drawable/ic_favorite_24dp"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
/>
<Button
...
app:icon="@drawable/ic_remove_red_eye_24dp"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
/>
<Button
...
app:icon="@drawable/ic_notifications_24dp"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Anatomy and key properties
A toggle button has a shared stroked container, icons and/or text labels.
- Container
- Icon
Selection attributes
Element | Attribute | Related method(s) | Default value |
---|---|---|---|
Single selection | app:singleSelection | setSingleSelection isSingleSelection | false |
Selection required | app:selectionRequired | setSelectionRequired isSelectionRequired | false |
Styles
Element | Style |
---|---|
Default style | Widget.MaterialComponents.MaterialButtonToggleGroup |