11<!-- docs:
2- title: "Material selection controls: Switches "
2+ title: "Switch "
33layout: detail
44section: components
55excerpt: "Selection controls allow the user to select options."
66iconId: switch
77path: /catalog/switches/
88-->
99
10- # Selection controls: switches
11-
12- [ Selection controls] ( https://material.io/components/selection-controls#usage )
13- allow the user to select options.
10+ # Switch
1411
1512[ Switches] ( https://m3.material.io/components/switch/overview ) toggle the state
1613of a single setting on or off. They are the preferred way to adjust settings on
1714mobile devices.
15+ [ Selection controls] ( https://m3.material.io/components/#selection ) allow the
16+ user to select options.
1817
1918![ White "Settings" menu with purple header and switches to turn on options, such
2019as "Wi-fi" and "Bluetooth"] ( assets/switch/switch_hero.png )
2120
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.
3022
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.
3527
3628Before 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
3830[ Getting started] ( https://github.com/material-components/material-components-android/tree/master/docs/getting-started.md )
3931page.
4032
33+ Use switch to:
34+
35+ * Toggle a single item on or off, on mobile and tablet
36+ * Immediately activate or deactivate something
37+
4138** Note:** The ` MaterialSwitch ` widget provides a complete implementation of
4239Material Design's switch component. It extends from the support library's
4340` SwitchCompat ` widget, but not from the framework ` Switch ` widget. As such, it
4441does not auto-inflate, unlike other selection controls, and must be explicitly
4542specified in layouts.
4643
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-
7844** Note:** For the old ` SwitchMaterial ` documentation, please refer to
7945[ Switch (deprecated)] ( #switch-deprecated ) and
80- [ Theming switches (deprecated)] ( #theming-switches -deprecated ) .
46+ [ Theming switch (deprecated)] ( #theming-switch -deprecated ) .
8147
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
12649
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 )
12852
129- The following is an anatomy diagram that shows a switch thumb and a switch
130- track:
53+ ## Anatomy
13154
13255![ Switch anatomy diagram] ( assets/switch/switch_anatomy.png )
13356
134571 . 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
13765
13866### Switch attributes
13967
@@ -194,24 +122,88 @@ Element | Style
194122
195123Default style theme attribute: ` ?attr/materialSwitchStyle `
196124
197- See the full list of
125+ For the full list, see
198126[ styles] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/materialswitch/res/values/styles.xml )
199127and
200128[ attrs] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/materialswitch/res/values/attrs.xml ) .
201129
202- ## Theming switches
130+ ## Code implementation
203131
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.
207133
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 " />
209135
210- The following example shows a list of switches with Material Theming.
136+ In the layout:
211137
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+ ```
213162
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
215207
216208Use theme attributes in ` res/values/styles.xml ` , which applies to all switches
217209and affects other components:
@@ -253,19 +245,12 @@ Use the styles in the layout, which affects only this switch:
253245 />
254246```
255247
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 >
265250
266251The following example shows a list of five switches.
267252
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 " />
269254
270255In the layout:
271256
@@ -306,17 +291,16 @@ switchmaterial.setOnCheckedChangeListener { buttonView, isChecked
306291}
307292```
308293
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
313295
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 " />
315297
3162981 . Thumb
3172992 . Track
318300
319- ### Switch attributes (deprecated)
301+ ### Key properties
302+
303+ #### Switch attributes
320304
321305Element | Attribute | Related method(s) | Default value
322306-------------------------- | ------------------------------------------ | ---------------------------------------------------------- | -------------
@@ -336,30 +320,30 @@ with a custom drawable that should not be tinted, set
336320 />
337321```
338322
339- ### Thumb attributes (deprecated)
323+ #### Thumb attributes
340324
341325Element | Attribute | Related method(s) | Default value
342326------------- | --------------- | ----------------------------------------- | -------------
343327** Thumb** | ` android:thumb ` | ` setThumbDrawable ` <br />` getThumbDrawable ` | inherits from ` SwitchCompat `
344328** Color** | ` app:thumbTint ` | ` setThumbTintList ` <br />` getThumbTintList ` | ` ?attr/colorOnSurface ` (unchecked)<br />` ?attr/colorPrimary ` (checked)
345329** Elevation** | N/A | N/A | ` 4dp `
346330
347- ### Track attributes (deprecated)
331+ #### Track attributes
348332
349333Element | Attribute | Related method(s) | Default value
350334--------- | --------------- | ----------------------------------------- | -------------
351335** Track** | ` app:track ` | ` setTrackDrawable ` <br />` getTrackDrawable ` | inherits from ` SwitchCompat `
352336** Color** | ` app:trackTint ` | ` setTrackTintList ` <br />` getTrackTintList ` | ` ?attr/colorOutline ` (unchecked)<br />` ?attr/colorPrimaryContainer ` (checked)
353337
354- ### Text label attributes (deprecated)
338+ #### Text label attributes
355339
356340Element | Attribute | Related method(s) | Default value
357341-------------- | ------------------------ | ---------------------------------- | -------------
358342** Text label** | ` android:text ` | ` setText ` <br />` getText ` | ` null `
359343** Color** | ` android:textColor ` | ` setTextColor ` <br />` getTextColors ` | ` ?android:attr/textColorPrimaryDisableOnly `
360344** Typography** | ` android:textAppearance ` | ` setTextAppearance ` | ` ?attr/textAppearanceBodyMedium `
361345
362- ### Switch states (deprecated)
346+ #### Switch states
363347
364348Switches can be on or off. Switches have enabled, hover, focused, and pressed
365349states.
@@ -372,38 +356,36 @@ For desktop, the radial reaction isn't needed.
372356![ Switch states in an array. Columns are enabled, disabled, hover, focused,
373357pressed. Rows are on or off] ( assets/switch/switch_states_deprecated.png )
374358
375- ### Styles (deprecated)
359+ #### Styles
376360
377361Element | Style
378362----------------- | ----------------------------------------
379363** Default style** | ` Widget.Material3.CompoundButton.Switch `
380364
381365Default style theme attribute: ` ?attr/switchStyle `
382366
383- See the full list of
367+ For the full list, see
384368[ styles] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/res/values/styles.xml )
385369and
386370[ attrs] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/res/values/attrs.xml ) .
387371
388- ## Theming switches (deprecated)
372+ < h3 id = " theming-switch-deprecated " > Theming switch</ h3 >
389373
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.
393375
394- ### Switch theming example (deprecated)
376+ #### Switch theming example
395377
396378API and source code:
397379
398380* ` SwitchMaterial `
399381 * [ Class definition] ( https://developer.android.com/reference/com/google/android/material/switchmaterial/SwitchMaterial )
400382 * [ Class source] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/SwitchMaterial.java )
401383
402- The following example shows a list of switches with Material Theming .
384+ The following example shows a list of switches with Material theming .
403385
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 " />
405387
406- #### Implementing switch theming (deprecated)
388+ ##### Implementing switch theming
407389
408390Use theme attributes in ` res/values/styles.xml ` , which applies to all switches
409391and affects other components:
@@ -444,3 +426,5 @@ Use the styles in the layout, which affects only this switch:
444426 style =" @style/Widget.App.Switch"
445427 />
446428```
429+
430+ </details >
0 commit comments