Skip to content

Commit 0c7db72

Browse files
leticiarossiymarian
authored andcommitted
[Documentation] Updating Switch documentation.
PiperOrigin-RevId: 314165848
1 parent 675fe8c commit 0c7db72

File tree

6 files changed

+213
-31
lines changed

6 files changed

+213
-31
lines changed

docs/components/Switch.md

Lines changed: 213 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,245 @@
11
<!--docs:
2-
title: "Switches"
2+
title: "Material selection controls: Switches"
33
layout: detail
44
section: components
5-
excerpt: "Switches are toggleable buttons with two states: on and off (selected and unselected)."
5+
excerpt: "Selection controls allow the user to select options."
66
iconId: switch
77
path: /catalog/switch/
88
-->
99

10-
# Switches
10+
# Selection controls: switches
11+
12+
[Selection controls](https://material.io/components/selection-controls#usage)
13+
allow the user to select options.
14+
15+
Switches toggle the state of a single setting on or off. They are the preferred
16+
way to adjust settings on mobile.
17+
18+
![Switch hero example for menu options](assets/switch/switch_hero.png)
19+
20+
## Contents
21+
22+
* [Using switches](#using-switches)
23+
* [Switch](#switch)
24+
* [Theming switches](#theming-switches)
25+
26+
## Using switches
27+
28+
Before you can use Material switches, you need to add a dependency to the
29+
Material Components for Android library. For more information, go to the
30+
[Getting started](/material-components/material-components-android/blob/master/docs/getting-started.md)
31+
page.
32+
33+
_**Note:** The `SwitchMaterial` widget provides a complete implementation of
34+
Material Design's switch component. It extends from the support library's
35+
`SwitchCompat` widget, but not from the framework `Switch` widget. As such, it
36+
does not auto-inflate, unlike other selection controls, and must be explicitly
37+
specified in layouts._
38+
39+
Use switches to:
40+
41+
* Toggle a single item on or off, on mobile and tablet
42+
* Immediately activate or deactivate something
43+
44+
### Making switches accessible
45+
46+
Switches support content labeling for accessibility and are readable by most
47+
screen readers, such as TalkBack. Text rendered in switches is automatically
48+
provided to accessibility services. Additional content labels are usually
49+
unnecessary.
50+
51+
## Switch
1152

1253
A `Switch` represents a button with two states, on and off. Switches are most
1354
often used on mobile devices to enable and disable options in an options menu. A
1455
switch consists of a track and thumb; the thumb moves along the track to
1556
indicate its current state.
1657

17-
## Design & API Documentation
58+
### Switches example
59+
60+
API and source code:
1861

19-
- [Material Design guidelines: Switches](https://material.io/go/design-switches)
20-
<!--{: .icon-list-item.icon-list-item--spec }-->
21-
- [Class definition](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/SwitchMaterial.java)
22-
<!--{: .icon-list-item.icon-list-item--link }-->
23-
- [Class overview](https://developer.android.com/reference/android/support/v7/widget/SwitchCompat)
24-
<!--{: .icon-list-item.icon-list-item--link }--> <!--{: .icon-list }-->
62+
* `SwitchMaterial`
63+
* [Class definition](https://developer.android.com/reference/com/google/android/material/switchmaterial/SwitchMaterial)
64+
* [Class source](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/SwitchMaterial.java)
2565

26-
## Usage
66+
The following example shows a list of five switches.
2767

28-
The `SwitchMaterial` widget provides a complete implementation of Material
29-
Design's switch component. It extends from the support library's `SwitchCompat`
30-
widget, but not from the framework Switch widget. As such, it does not
31-
auto-inflate, unlike other selection controls, and must be explicitly specified
32-
in layouts.
68+
![Example of 5 switches, the first one is toggled and the last one is disabled.](assets/switch/switch_example.png)
69+
70+
In the layout:
3371

3472
```xml
3573
<com.google.android.material.switchmaterial.SwitchMaterial
36-
android:layout_width="match_parent"
37-
android:layout_height="match_parent"
38-
android:checked="true"
39-
android:text="@string/switch_text"/>
74+
android:layout_width="wrap_content"
75+
android:layout_height="match_parent"
76+
android:checked="true"
77+
android:text="@string/label_1"/>
78+
<com.google.android.material.switchmaterial.SwitchMaterial
79+
android:layout_width="wrap_content"
80+
android:layout_height="match_parent"
81+
android:text="@string/label_2"/>
82+
<com.google.android.material.switchmaterial.SwitchMaterial
83+
android:layout_width="wrap_content"
84+
android:layout_height="match_parent"
85+
android:text="@string/label_3"/>
86+
<com.google.android.material.switchmaterial.SwitchMaterial
87+
android:layout_width="wrap_content"
88+
android:layout_height="match_parent"
89+
android:text="@string/label_4"/>
90+
<com.google.android.material.switchmaterial.SwitchMaterial
91+
android:layout_width="wrap_content"
92+
android:layout_height="match_parent"
93+
android:enabled="false"
94+
android:text="@string/label_5"/>
95+
```
96+
97+
In code:
98+
99+
```kt
100+
// To check a switch
101+
switchMaterial.isChecked = true
102+
103+
// To listen for a switch's checked/unchecked state changes
104+
switchMaterial.setOnCheckedChangeListener { buttonView, isChecked
105+
// Responds to switch being checked/unchecked
106+
}
40107
```
41108

42-
### Material Styles
109+
## Anatomy and key properties
110+
111+
The following is an anatomy diagram that shows a switch thumb and a switch
112+
track:
43113

44-
Using a Material Components theme with `SwitchMaterial` will match the color of
45-
`SwitchMaterial` views to your theme's palette. If you want to override this
46-
behavior, as you might with custom drawables, set the `useMaterialThemeColors`
47-
parameter to false.
114+
![Switch anatomy diagram](assets/switch/switch_anatomy.png)
115+
116+
1. Thumb
117+
1. Track
118+
119+
### Switch attributes
120+
121+
&nbsp; | Attribute | Related method(s) | Default value
122+
-------------------------- | ------------------------------------------ | ---------------------------------------------------------- | -------------
123+
**To use material colors** | `app:useMaterialThemeColors` | `setUseMaterialThemeColors`<br/>`isUseMaterialThemeColors` | `true` (ignored if specific tint attrs are set)
124+
**Min size** | `android:minWidth`<br/>`android:minHeight` | `(set/get)MinWidth`<br/>`(set/get)MinHeight` | `?attr/minTouchTargetSize`
125+
126+
The color of the switch defaults to using `?attr/colorSurface`,
127+
`?attr/colorOnSurface`, and `?attr/colorSecondary` defined in your app theme. If
128+
you want to override this behavior, as you might with a custom drawable that
129+
should not be tinted, set `app:useMaterialThemeColors` to `false`:
48130

49131
```xml
50132
<com.google.android.material.switchmaterial.SwitchMaterial
51-
xmlns:android="http://schemas.android.com/apk/res/android"
52-
xmlns:app="http://schemas.android.com/apk/res-auto"
53-
app:useMaterialThemeColors="false"
54-
android:text="@string/switch_text"/>
133+
...
134+
app:useMaterialThemeColors="false"
135+
/>
55136
```
56137

138+
### Thumb attributes
139+
140+
&nbsp; | Attribute | Related method(s) | Default value
141+
------------- | --------------- | ----------------------------------------- | -------------
142+
**Thumb** | `android:thumb` | `setThumbDrawable`<br/>`getThumbDrawable` | inherits from `SwitchCompat`
143+
**Color** | `app:thumbTint` | `setThumbTintList`<br/>`getThumbTintList` | `?attr/colorSurface` (unchecked)<br/>`?attr/colorSecondary` (checked)
144+
**Elevation** | N/A | N/A | `4dp`
145+
146+
### Track attributes
147+
148+
&nbsp; | Attribute | Related method(s) | Default value
149+
--------- | --------------- | ----------------------------------------- | -------------
150+
**Track** | `app:track` | `setTrackDrawable`<br/>`getTrackDrawable` | inherits from `SwitchCompat`
151+
**Color** | `app:trackTint` | `setTrackTintList`<br/>`getTrackTintList` | `?attr/colorOnSurface` (unchecked)<br/>`?attr/colorSecondary` (checked)
152+
153+
### Text label attributes
154+
155+
&nbsp; | Attribute | Related method(s) | Default value
156+
-------------- | ------------------------ | ---------------------------------- | -------------
157+
**Text label** | `android:text` | `setText`<br/>`getText` | `null`
158+
**Color** | `android:textColor` | `setTextColor`<br/>`getTextColors` | inherits from `SwitchCompat`
159+
**Typography** | `android:textAppearance` | `setTextAppearance` | inherits from `SwitchCompat`
160+
161+
### Switch states
162+
163+
Switches can be on or off. Switches have enabled, hover, focused, and pressed
164+
states.
165+
166+
Display the outer radial reaction only on form factors that use touch, where
167+
interaction may obstruct the element completely.
168+
169+
For desktop, the radial reaction isn't needed.
170+
171+
![Switch states in an array. Columns are enabled, disabled, hover, focused,
172+
pressed. Rows are on or off](assets/switch/switch_states.png)
173+
57174
### Styles
58175

59-
Use `switchStyle` for style changes.
176+
&nbsp; | Style
177+
----------------- | -------------------------------------------------
178+
**Default style** | `Widget.MaterialComponents.CompoundButton.Switch`
179+
180+
Default style theme attribute: `?attr/switchStyle`
181+
182+
See the full list of
183+
[styles](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/res/values/styles.xml)
184+
and
185+
[attrs](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/res/values/attrs.xml).
186+
187+
## Theming switches
188+
189+
Switches support
190+
[Material Theming](https://material.io/components/selection-controls#theming)
191+
and can be customized in terms of color and typography.
192+
193+
### Switch theming example
194+
195+
API and source code:
196+
197+
* `SwitchMaterial`
198+
* [Class definition](https://developer.android.com/reference/com/google/android/material/switchmaterial/SwitchMaterial)
199+
* [Class source](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/SwitchMaterial.java)
200+
201+
The following example shows a list of switches with Material Theming.
202+
203+
!["Switch theming with pink and brown colors"](assets/switch/switch_theming.png)
204+
205+
#### Implementing switch theming
206+
207+
Using theme attributes in `res/values/styles.xml` (themes all switches and
208+
affects other components):
209+
210+
```xml
211+
<style name="Theme.App" parent="Theme.MaterialComponents.*">
212+
...
213+
<item name="colorOnSurface">@color/shrine_pink_900</item>
214+
<item name="colorSecondary">@color/shrine_pink_100</item>
215+
</style>
216+
217+
```
218+
219+
or using default style theme attributes, styles and theme overlays (themes all
220+
switches but does not affect other components):
60221

61222
```xml
62-
<item name="switchStyle">@style/Widget.MaterialComponents.CompoundButton.Switch</item>
223+
<style name="Theme.App" parent="Theme.MaterialComponents.*">
224+
...
225+
<item name="switchStyle">@style/Widget.App.Switch</item>
226+
</style>
227+
228+
<style name="Widget.App.Switch" parent="Widget.MaterialComponents.CompoundButton.Switch">
229+
<item name="materialThemeOverlay">@style/ThemeOverlay.App.Switch</item>
230+
</style>
231+
232+
<style name="ThemeOverlay.App.Switch" parent="">
233+
<item name="colorOnSurface">@color/shrine_pink_900</item>
234+
<item name="colorSecondary">@color/shrine_pink_100</item>
235+
</style>
236+
```
237+
238+
or using the styles in the layout (affects only this switch):
239+
240+
```xml
241+
<com.google.android.material.switchmaterial.SwitchMaterial
242+
...
243+
style="@style/Widget.App.Switch"
244+
/>
63245
```
18.8 KB
Loading
15 KB
Loading
28.9 KB
Loading
42.2 KB
Loading
15.1 KB
Loading

0 commit comments

Comments
 (0)