1
1
<!-- docs:
2
- title: "Material selection controls: Switches "
2
+ title: "Switch "
3
3
layout: detail
4
4
section: components
5
5
excerpt: "Selection controls allow the user to select options."
6
6
iconId: switch
7
7
path: /catalog/switches/
8
8
-->
9
9
10
- # Selection controls: switches
11
-
12
- [ Selection controls] ( https://material.io/components/selection-controls#usage )
13
- allow the user to select options.
10
+ # Switch
14
11
15
12
[ Switches] ( https://m3.material.io/components/switch/overview ) toggle the state
16
13
of a single setting on or off. They are the preferred way to adjust settings on
17
14
mobile devices.
15
+ [ Selection controls] ( https://m3.material.io/components/#selection ) allow the
16
+ user to select options.
18
17
19
18
![ White "Settings" menu with purple header and switches to turn on options, such
20
19
as "Wi-fi" and "Bluetooth"] ( assets/switch/switch_hero.png )
21
20
22
- ** Contents**
23
-
24
- * [ Design and API Documentation] ( #design-and-api-documentation )
25
- * [ Using switches] ( #using-switches )
26
- * [ Switch] ( #switch )
27
- * [ Theming switches] ( #theming-switches )
28
-
29
- ## Design and API Documentation
21
+ ** Note:** Images use various dynamic color schemes.
30
22
31
- * [ Google Material3 Spec ] ( https://material.io/components/switch/overview )
32
- * [ API Reference ] ( https://developer.android.com/reference/com/google/android/material/switchmaterial/package-summary )
33
-
34
- ## Using switches
23
+ A ` Switch ` represents a button with two states, on and off. Switches are most
24
+ often used on mobile devices to enable and disable options in an options menu. A
25
+ switch consists of a track and thumb; the thumb moves along the track to
26
+ indicate its current state.
35
27
36
28
Before you can use Material switches, you need to add a dependency on the
37
- Material Components for Android library. For more information, go to the
29
+ Material components for Android library. For more information, go to the
38
30
[ Getting started] ( https://github.com/material-components/material-components-android/tree/master/docs/getting-started.md )
39
31
page.
40
32
33
+ Use switch to:
34
+
35
+ * Toggle a single item on or off, on mobile and tablet
36
+ * Immediately activate or deactivate something
37
+
41
38
** Note:** The ` MaterialSwitch ` widget provides a complete implementation of
42
39
Material Design's switch component. It extends from the support library's
43
40
` SwitchCompat ` widget, but not from the framework ` Switch ` widget. As such, it
44
41
does not auto-inflate, unlike other selection controls, and must be explicitly
45
42
specified in layouts.
46
43
47
- Use switches to:
48
-
49
- * Toggle a single item on or off, on mobile and tablet
50
- * Immediately activate or deactivate something
51
-
52
- ### Making switches accessible
53
-
54
- Switches support content labeling for accessibility and are readable by most
55
- screen readers, such as TalkBack. Text rendered in switches is automatically
56
- provided to accessibility services. Additional content labels are usually
57
- unnecessary.
58
-
59
- ## Switch
60
-
61
- A ` Switch ` represents a button with two states, on and off. Switches are most
62
- often used on mobile devices to enable and disable options in an options menu. A
63
- switch consists of a track and thumb; the thumb moves along the track to
64
- indicate its current state.
65
-
66
- API and source code:
67
-
68
- * ` MaterialSwitch `
69
- * [ Class definition] ( https://developer.android.com/reference/com/google/android/material/materialswitch/MaterialSwitch )
70
- * [ Class source] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/materialswitch/MaterialSwitch.java )
71
-
72
- ** Note:** Since version 1.7.0, the new ` MaterialSwitch ` class will replace the
73
- obsolete ` SwitchMaterial ` class. In most cases you should be able to just
74
- replace all ` SwitchMaterial ` class reference with ` MaterialSwitch ` to achieve
75
- the default look and feel. Please refer to the following sections if you need to
76
- customize the new styles.
77
-
78
44
** Note:** For the old ` SwitchMaterial ` documentation, please refer to
79
45
[ Switch (deprecated)] ( #switch-deprecated ) and
80
- [ Theming switches (deprecated)] ( #theming-switches -deprecated ) .
46
+ [ Theming switch (deprecated)] ( #theming-switch -deprecated ) .
81
47
82
- ### Switches example
83
-
84
- The following example shows a list of five switches.
85
-
86
- ![ Example of 5 switches, the first one is toggled and the last one is disabled.] ( assets/switch/switch_example.png )
87
-
88
- In the layout:
89
-
90
- ``` xml
91
- <com .google.android.material.materialswitch.MaterialSwitch
92
- android : layout_width =" wrap_content"
93
- android : layout_height =" match_parent"
94
- android : checked =" true"
95
- android : text =" @string/label_1" />
96
- <com .google.android.material.materialswitch.MaterialSwitch
97
- android : layout_width =" wrap_content"
98
- android : layout_height =" match_parent"
99
- android : text =" @string/label_2" />
100
- <com .google.android.material.materialswitch.MaterialSwitch
101
- android : layout_width =" wrap_content"
102
- android : layout_height =" match_parent"
103
- android : text =" @string/label_3" />
104
- <com .google.android.material.materialswitch.MaterialSwitch
105
- android : layout_width =" wrap_content"
106
- android : layout_height =" match_parent"
107
- android : text =" @string/label_4" />
108
- <com .google.android.material.materialswitch.MaterialSwitch
109
- android : layout_width =" wrap_content"
110
- android : layout_height =" match_parent"
111
- android : enabled =" false"
112
- android : text =" @string/label_5" />
113
- ```
114
-
115
- In code:
116
-
117
- ``` kt
118
- // To check a switch
119
- materialSwitch.isChecked = true
120
-
121
- // To listen for a switch's checked/unchecked state changes
122
- materialSwitch.setOnCheckedChangeListener { buttonView, isChecked
123
- // Responds to switch being checked/unchecked
124
- }
125
- ```
48
+ ## Design & API documentation
126
49
127
- ## Anatomy and key properties
50
+ * [ Material 3 (M3) spec] ( https://m3.material.io/components/switch/overview )
51
+ * [ API reference] ( https://developer.android.com/reference/com/google/android/material/switchmaterial/package-summary )
128
52
129
- The following is an anatomy diagram that shows a switch thumb and a switch
130
- track:
53
+ ## Anatomy
131
54
132
55
![ Switch anatomy diagram] ( assets/switch/switch_anatomy.png )
133
56
134
57
1 . Track
135
- 2 . Thumb
136
- 3 . Icon (optional)
58
+ 2 . Handle (formerly "thumb")
59
+ 3 . Icon
60
+
61
+ More details on anatomy items in the
62
+ [ component guidelines] ( https://m3.material.io/components/switch/guidelines#9f55e13e-1327-4edf-9b81-6fa97db45bdd ) .
63
+
64
+ ## Key properties
137
65
138
66
### Switch attributes
139
67
@@ -194,24 +122,88 @@ Element | Style
194
122
195
123
Default style theme attribute: ` ?attr/materialSwitchStyle `
196
124
197
- See the full list of
125
+ For the full list, see
198
126
[ styles] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/materialswitch/res/values/styles.xml )
199
127
and
200
128
[ attrs] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/materialswitch/res/values/attrs.xml ) .
201
129
202
- ## Theming switches
130
+ ## Code implementation
203
131
204
- Switches support
205
- [ Material Theming] ( https://material.io/components/selection-controls#theming ) ,
206
- which can customize color and typography.
132
+ The following example shows a list of five switches.
207
133
208
- ### Switch theming example
134
+ < img src = " assets/switch/switch_example.png " alt = " Example of 5 switches, the first one is toggled and the last one is disabled. " height = " 350 " />
209
135
210
- The following example shows a list of switches with Material Theming.
136
+ In the layout:
211
137
212
- ![ "5 switches with brown text: first switch is on and has pink thumb and track"] ( assets/switch/switch_theming.png )
138
+ ``` xml
139
+ <com .google.android.material.materialswitch.MaterialSwitch
140
+ android : layout_width =" wrap_content"
141
+ android : layout_height =" match_parent"
142
+ android : checked =" true"
143
+ android : text =" @string/label_1" />
144
+ <com .google.android.material.materialswitch.MaterialSwitch
145
+ android : layout_width =" wrap_content"
146
+ android : layout_height =" match_parent"
147
+ android : text =" @string/label_2" />
148
+ <com .google.android.material.materialswitch.MaterialSwitch
149
+ android : layout_width =" wrap_content"
150
+ android : layout_height =" match_parent"
151
+ android : text =" @string/label_3" />
152
+ <com .google.android.material.materialswitch.MaterialSwitch
153
+ android : layout_width =" wrap_content"
154
+ android : layout_height =" match_parent"
155
+ android : text =" @string/label_4" />
156
+ <com .google.android.material.materialswitch.MaterialSwitch
157
+ android : layout_width =" wrap_content"
158
+ android : layout_height =" match_parent"
159
+ android : enabled =" false"
160
+ android : text =" @string/label_5" />
161
+ ```
213
162
214
- #### Implementing switch theming
163
+ In code:
164
+
165
+ ``` kt
166
+ // To check a switch
167
+ materialSwitch.isChecked = true
168
+
169
+ // To listen for a switch's checked/unchecked state changes
170
+ materialSwitch.setOnCheckedChangeListener { buttonView, isChecked
171
+ // Responds to switch being checked/unchecked
172
+ }
173
+ ```
174
+
175
+ ## Making switch accessible
176
+
177
+ Switches support content labeling for accessibility and are readable by most
178
+ screen readers, such as Talkback. Text rendered in switches is automatically
179
+ provided to accessibility services. Additional content labels are usually
180
+ unnecessary.
181
+
182
+ ## Customizing switch
183
+
184
+ ### Theming switch
185
+
186
+ Switch supports the customization of color and typography.
187
+
188
+ #### Switch theming example
189
+
190
+ API and source code:
191
+
192
+ * ` MaterialSwitch `
193
+ * [ Class definition] ( https://developer.android.com/reference/com/google/android/material/materialswitch/MaterialSwitch )
194
+ * [ Class source] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/materialswitch/MaterialSwitch.java )
195
+
196
+ ** Note:** Since version 1.7.0, the new ` MaterialSwitch ` class will replace the
197
+ obsolete ` SwitchMaterial ` class. In most cases you should be able to just
198
+ replace all ` SwitchMaterial ` class reference with ` MaterialSwitch ` to achieve
199
+ the default look and feel. Please refer to the following sections if you need to
200
+ customize the new styles.
201
+
202
+ The following example shows a list of switches with Material theming.
203
+
204
+ <img src =" assets/switch/switch_theming.png " alt =" 5 switches with brown text: first switch is on and has pink thumb and track " height =" 350 " />
205
+
206
+ ##### Implementing switch theming
215
207
216
208
Use theme attributes in ` res/values/styles.xml ` , which applies to all switches
217
209
and affects other components:
@@ -253,19 +245,12 @@ Use the styles in the layout, which affects only this switch:
253
245
/>
254
246
```
255
247
256
- ## Switch (deprecated)
257
-
258
- ### Switches example (deprecated)
259
-
260
- API and source code:
261
-
262
- * ` SwitchMaterial `
263
- * [ Class definition] ( https://developer.android.com/reference/com/google/android/material/switchmaterial/SwitchMaterial )
264
- * [ Class source] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/SwitchMaterial.java )
248
+ <details >
249
+ <summary ><h2 id =" switch-deprecated " >Switch (deprecated)</h2 ></summary >
265
250
266
251
The following example shows a list of five switches.
267
252
268
- ![ Example of 5 switches, the first one is toggled and the last one is disabled.] ( assets/switch/switch_example_deprecated.png )
253
+ < img src = " assets/switch/switch_example_deprecated.png " alt = " Example of 5 switches, the first one is toggled and the last one is disabled. " height = " 350 " />
269
254
270
255
In the layout:
271
256
@@ -306,17 +291,16 @@ switchmaterial.setOnCheckedChangeListener { buttonView, isChecked
306
291
}
307
292
```
308
293
309
- ## Anatomy and key properties (deprecated)
310
-
311
- The following is an anatomy diagram that shows a switch thumb and a switch
312
- track:
294
+ ### Anatomy
313
295
314
- ![ Switch anatomy diagram ] ( assets/switch/switch_anatomy_deprecated.png )
296
+ < img src = " assets/switch/switch_anatomy_deprecated.png " alt = " Switch anatomy deprecated diagram " width = " 500 " />
315
297
316
298
1 . Thumb
317
299
2 . Track
318
300
319
- ### Switch attributes (deprecated)
301
+ ### Key properties
302
+
303
+ #### Switch attributes
320
304
321
305
Element | Attribute | Related method(s) | Default value
322
306
-------------------------- | ------------------------------------------ | ---------------------------------------------------------- | -------------
@@ -336,30 +320,30 @@ with a custom drawable that should not be tinted, set
336
320
/>
337
321
```
338
322
339
- ### Thumb attributes (deprecated)
323
+ #### Thumb attributes
340
324
341
325
Element | Attribute | Related method(s) | Default value
342
326
------------- | --------------- | ----------------------------------------- | -------------
343
327
** Thumb** | ` android:thumb ` | ` setThumbDrawable ` <br />` getThumbDrawable ` | inherits from ` SwitchCompat `
344
328
** Color** | ` app:thumbTint ` | ` setThumbTintList ` <br />` getThumbTintList ` | ` ?attr/colorOnSurface ` (unchecked)<br />` ?attr/colorPrimary ` (checked)
345
329
** Elevation** | N/A | N/A | ` 4dp `
346
330
347
- ### Track attributes (deprecated)
331
+ #### Track attributes
348
332
349
333
Element | Attribute | Related method(s) | Default value
350
334
--------- | --------------- | ----------------------------------------- | -------------
351
335
** Track** | ` app:track ` | ` setTrackDrawable ` <br />` getTrackDrawable ` | inherits from ` SwitchCompat `
352
336
** Color** | ` app:trackTint ` | ` setTrackTintList ` <br />` getTrackTintList ` | ` ?attr/colorOutline ` (unchecked)<br />` ?attr/colorPrimaryContainer ` (checked)
353
337
354
- ### Text label attributes (deprecated)
338
+ #### Text label attributes
355
339
356
340
Element | Attribute | Related method(s) | Default value
357
341
-------------- | ------------------------ | ---------------------------------- | -------------
358
342
** Text label** | ` android:text ` | ` setText ` <br />` getText ` | ` null `
359
343
** Color** | ` android:textColor ` | ` setTextColor ` <br />` getTextColors ` | ` ?android:attr/textColorPrimaryDisableOnly `
360
344
** Typography** | ` android:textAppearance ` | ` setTextAppearance ` | ` ?attr/textAppearanceBodyMedium `
361
345
362
- ### Switch states (deprecated)
346
+ #### Switch states
363
347
364
348
Switches can be on or off. Switches have enabled, hover, focused, and pressed
365
349
states.
@@ -372,38 +356,36 @@ For desktop, the radial reaction isn't needed.
372
356
![ Switch states in an array. Columns are enabled, disabled, hover, focused,
373
357
pressed. Rows are on or off] ( assets/switch/switch_states_deprecated.png )
374
358
375
- ### Styles (deprecated)
359
+ #### Styles
376
360
377
361
Element | Style
378
362
----------------- | ----------------------------------------
379
363
** Default style** | ` Widget.Material3.CompoundButton.Switch `
380
364
381
365
Default style theme attribute: ` ?attr/switchStyle `
382
366
383
- See the full list of
367
+ For the full list, see
384
368
[ styles] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/res/values/styles.xml )
385
369
and
386
370
[ attrs] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/res/values/attrs.xml ) .
387
371
388
- ## Theming switches (deprecated)
372
+ < h3 id = " theming-switch-deprecated " > Theming switch</ h3 >
389
373
390
- Switches support
391
- [ Material Theming] ( https://material.io/components/selection-controls#theming ) ,
392
- which can customize color and typography.
374
+ Switch supports the customization of color and typography.
393
375
394
- ### Switch theming example (deprecated)
376
+ #### Switch theming example
395
377
396
378
API and source code:
397
379
398
380
* ` SwitchMaterial `
399
381
* [ Class definition] ( https://developer.android.com/reference/com/google/android/material/switchmaterial/SwitchMaterial )
400
382
* [ Class source] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/SwitchMaterial.java )
401
383
402
- The following example shows a list of switches with Material Theming .
384
+ The following example shows a list of switches with Material theming .
403
385
404
- ![ " 5 switches with brown text: first switch is on and has pink thumb and track"] ( assets/switch/switch_theming_deprecated.png )
386
+ < img src = " assets/switch/switch_theming_deprecated.png " alt = " 5 switches with brown text: first switch is on and has pink thumb and track " height = " 350 " />
405
387
406
- #### Implementing switch theming (deprecated)
388
+ ##### Implementing switch theming
407
389
408
390
Use theme attributes in ` res/values/styles.xml ` , which applies to all switches
409
391
and affects other components:
@@ -444,3 +426,5 @@ Use the styles in the layout, which affects only this switch:
444
426
style =" @style/Widget.App.Switch"
445
427
/>
446
428
```
429
+
430
+ </details >
0 commit comments