@@ -31,7 +31,7 @@ Material Components for Android library. For more information, go to the
31
31
[ Getting started] ( https://github.com/material-components/material-components-android/tree/master/docs/getting-started.md )
32
32
page.
33
33
34
- ** Note:** The ` SwitchMaterial ` widget provides a complete implementation of
34
+ ** Note:** The ` MaterialSwitch ` widget provides a complete implementation of
35
35
Material Design's switch component. It extends from the support library's
36
36
` SwitchCompat ` widget, but not from the framework ` Switch ` widget. As such, it
37
37
does not auto-inflate, unlike other selection controls, and must be explicitly
@@ -56,8 +56,193 @@ often used on mobile devices to enable and disable options in an options menu. A
56
56
switch consists of a track and thumb; the thumb moves along the track to
57
57
indicate its current state.
58
58
59
+ ** Note:** Since version 1.7.0, the new ` MaterialSwitch ` class will replace the
60
+ obsolete ` SwitchMaterial ` class. In most cases you should be able to just
61
+ replace all ` SwitchMaterial ` class reference with ` MaterialSwitch ` to enjoy the
62
+ default look and feel. Please refer to the following sections if you need to
63
+ customize the new styles.
64
+
65
+ ** Note:** For the old ` SwitchMaterial ` documentation, please refer to
66
+ [ Switch (deprecated)] ( #switch-deprecated ) and
67
+ [ Theming switches (deprecated)] ( #theming-switches-deprecated ) .
68
+
59
69
### Switches example
60
70
71
+ The following example shows a list of five switches.
72
+
73
+ ![ Example of 5 switches, the first one is toggled and the last one is disabled.] ( assets/switch/switch_example.png )
74
+
75
+ In the layout:
76
+
77
+ ``` xml
78
+ <com .google.android.material.materialswitch.MaterialSwitch
79
+ android : layout_width =" wrap_content"
80
+ android : layout_height =" match_parent"
81
+ android : checked =" true"
82
+ android : text =" @string/label_1" />
83
+ <com .google.android.material.materialswitch.MaterialSwitch
84
+ android : layout_width =" wrap_content"
85
+ android : layout_height =" match_parent"
86
+ android : text =" @string/label_2" />
87
+ <com .google.android.material.materialswitch.MaterialSwitch
88
+ android : layout_width =" wrap_content"
89
+ android : layout_height =" match_parent"
90
+ android : text =" @string/label_3" />
91
+ <com .google.android.material.materialswitch.MaterialSwitch
92
+ android : layout_width =" wrap_content"
93
+ android : layout_height =" match_parent"
94
+ android : text =" @string/label_4" />
95
+ <com .google.android.material.materialswitch.MaterialSwitch
96
+ android : layout_width =" wrap_content"
97
+ android : layout_height =" match_parent"
98
+ android : enabled =" false"
99
+ android : text =" @string/label_5" />
100
+ ```
101
+
102
+ In code:
103
+
104
+ ``` kt
105
+ // To check a switch
106
+ materialSwitch.isChecked = true
107
+
108
+ // To listen for a switch's checked/unchecked state changes
109
+ materialSwitch.setOnCheckedChangeListener { buttonView, isChecked
110
+ // Responds to switch being checked/unchecked
111
+ }
112
+ ```
113
+
114
+ ## Anatomy and key properties
115
+
116
+ The following is an anatomy diagram that shows a switch thumb and a switch
117
+ track:
118
+
119
+ ![ Switch anatomy diagram] ( assets/switch/switch_anatomy.png )
120
+
121
+ 1 . Thumb
122
+ 2 . Track
123
+ 3 . Icon (optional)
124
+
125
+ ### Switch attributes
126
+
127
+ Element | Attribute | Related method(s) | Default value
128
+ -------------- | ------------------- | --------------------------------- | -------------
129
+ ** Min height** | ` android:minHeight ` | ` setMinHeight ` <br />` getMinHeight ` | ` ?attr/minTouchTargetSize `
130
+
131
+ ### Thumb attributes
132
+
133
+ Element | Attribute | Related method(s) | Default value
134
+ --------- | --------------- | ----------------------------------------- | -------------
135
+ ** Thumb** | ` android:thumb ` | ` setThumbDrawable ` <br />` getThumbDrawable ` | ` @drawable/mtrl_switch_thumb `
136
+ ** Color** | ` app:thumbTint ` | ` setThumbTintList ` <br />` getThumbTintList ` | ` ?attr/colorOutline ` (unchecked)<br />` ?attr/colorOnPrimary ` (checked)
137
+
138
+ ### Icon attributes
139
+
140
+ Element | Attribute | Related method(s) | Default value
141
+ --------- | ------------------- | ------------------------------------------------- | -------------
142
+ ** Icon** | ` app:thumbIcon ` | ` setThumbIconDrawable ` <br />` getThumbIconDrawable ` | ` null `
143
+ ** Color** | ` app:thumbIconTint ` | ` setThumbIconTintList ` <br />` getThumbIconTintList ` | ` ?attr/colorSurfaceVariant ` (unchecked)<br />` ?attr/colorOnPrimaryContainer ` (checked)
144
+
145
+ You can add an optional icon to enhance the on/off indication of your custom
146
+ switch by assiging ` app:thumbIcon ` . This icon will be centered and displayed on
147
+ top of the thumb drawable.
148
+
149
+ ### Track attributes
150
+
151
+ Element | Attribute | Related method(s) | Default value
152
+ -------------------- | ------------------------- | ------------------------------------------------------------- | -------------
153
+ ** Track** | ` app:track ` | ` setTrackDrawable ` <br />` getTrackDrawable ` | ` @drawable/mtrl_switch_track `
154
+ ** Color** | ` app:trackTint ` | ` setTrackTintList ` <br />` getTrackTintList ` | ` ?attr/colorSurfaceVariant ` (unchecked)<br />` ?attr/colorPrimary ` (checked)
155
+ ** Decoration** | ` app:trackDecoration ` | ` setTrackDecorationDrawable ` <br />` getTrackDecorationDrawable ` | ` @drawable/mtrl_switch_track_decoration ` <br />(Shows an outline of the track.)
156
+ ** Decoration color** | ` app:trackDecorationTint ` | ` setTrackDecorationTintList ` <br />` getTrackDecorationTintList ` | ` ?attr/colorOutline ` (unchecked)<br />` @android:color/transparent ` (checked)
157
+
158
+ ### Text label attributes
159
+
160
+ Element | Attribute | Related method(s) | Default value
161
+ -------------- | ------------------------ | ----------------------------------------- | -------------
162
+ ** Text label** | ` android:text ` | ` setText ` <br />` getText ` | ` null `
163
+ ** Color** | ` android:textColor ` | ` setTextColor ` <br />` getTextColors ` | ` ?android:attr/textColorPrimaryDisableOnly `
164
+ ** Typography** | ` android:textAppearance ` | ` setTextAppearance ` | ` ?attr/textAppearanceBodyMedium `
165
+ ** Padding** | ` app:switchPadding ` | ` setSwitchPadding ` <br />` getSwitchPadding ` | ` 16dp `
166
+
167
+ ### Switch states
168
+
169
+ Switches can be on or off. Switches have enabled, hover, focused, and pressed
170
+ states.
171
+
172
+ ![ Switch states in an array. Columns are enabled, disabled, hover, focused,
173
+ pressed. Rows are on or off] ( assets/switch/switch_states.png )
174
+
175
+ ### Styles
176
+
177
+ Element | Style
178
+ ----------------- | ------------------------------------------------
179
+ ** Default style** | ` Widget.Material3.CompoundButton.MaterialSwitch `
180
+
181
+ Default style theme attribute: ` ?attr/materialSwitchStyle `
182
+
183
+ See the full list of
184
+ [ styles] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/materialswitch/res/values/styles.xml )
185
+ and
186
+ [ attrs] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/materialswitch/res/values/attrs.xml ) .
187
+
188
+ ## Theming switches
189
+
190
+ Switches support
191
+ [ Material Theming] ( https://material.io/components/selection-controls#theming ) ,
192
+ which can customize color and typography.
193
+
194
+ ### Switch theming example
195
+
196
+ The following example shows a list of switches with Material Theming.
197
+
198
+ ![ "5 switches with brown text: first switch is on and has pink thumb and track"] ( assets/switch/switch_theming.png )
199
+
200
+ #### Implementing switch theming
201
+
202
+ Use theme attributes in ` res/values/styles.xml ` , which applies to all switches
203
+ and affects other components:
204
+
205
+ ``` xml
206
+ <style name =" Theme.App" parent =" Theme.Material3.*" >
207
+ ...
208
+ <item name =" colorPrimary" >@color/pink_200</item >
209
+ <item name =" colorSurfaceVariant" >@color/pink_100</item >
210
+ </style >
211
+
212
+ ```
213
+
214
+ Use default style theme attributes, styles and theme overlays, which apply to
215
+ all switches but do not affect other components:
216
+
217
+ ``` xml
218
+ <style name =" Theme.App" parent =" Theme.Material3.*" >
219
+ ...
220
+ <item name =" materialSwitchStyle" >@style/Widget.App.Switch</item >
221
+ </style >
222
+
223
+ <style name =" Widget.App.Switch" parent =" Widget.Material3.CompoundButton.MaterialSwitch" >
224
+ <item name =" materialThemeOverlay" >@style/ThemeOverlay.App.Switch</item >
225
+ </style >
226
+
227
+ <style name =" ThemeOverlay.App.Switch" parent =" " >
228
+ <item name =" colorPrimary" >@color/pink_200</item >
229
+ <item name =" colorSurfaceVariant" >@color/pink_100</item >
230
+ </style >
231
+ ```
232
+
233
+ Use the styles in the layout, which affects only this switch:
234
+
235
+ ``` xml
236
+ <com .google.android.material.materialswitch.MaterialSwitch
237
+ ...
238
+ style =" @style/Widget.App.Switch"
239
+ />
240
+ ```
241
+
242
+ ## Switch (deprecated)
243
+
244
+ ### Switches example (deprecated)
245
+
61
246
API and source code:
62
247
63
248
* ` SwitchMaterial `
@@ -66,7 +251,7 @@ API and source code:
66
251
67
252
The following example shows a list of five switches.
68
253
69
- ![ Example of 5 switches, the first one is toggled and the last one is disabled.] ( assets/switch/switch_example .png )
254
+ ![ Example of 5 switches, the first one is toggled and the last one is disabled.] ( assets/switch/switch_example_deprecated .png )
70
255
71
256
In the layout:
72
257
@@ -99,25 +284,25 @@ In code:
99
284
100
285
``` kt
101
286
// To check a switch
102
- switchMaterial .isChecked = true
287
+ switchmaterial .isChecked = true
103
288
104
289
// To listen for a switch's checked/unchecked state changes
105
- switchMaterial .setOnCheckedChangeListener { buttonView, isChecked
290
+ switchmaterial .setOnCheckedChangeListener { buttonView, isChecked
106
291
// Responds to switch being checked/unchecked
107
292
}
108
293
```
109
294
110
- ## Anatomy and key properties
295
+ ## Anatomy and key properties (deprecated)
111
296
112
297
The following is an anatomy diagram that shows a switch thumb and a switch
113
298
track:
114
299
115
- ![ Switch anatomy diagram] ( assets/switch/switch_anatomy .png )
300
+ ![ Switch anatomy diagram] ( assets/switch/switch_anatomy_deprecated .png )
116
301
117
302
1 . Thumb
118
303
2 . Track
119
304
120
- ### Switch attributes
305
+ ### Switch attributes (deprecated)
121
306
122
307
Element | Attribute | Related method(s) | Default value
123
308
-------------------------- | ------------------------------------------ | ---------------------------------------------------------- | -------------
@@ -137,30 +322,30 @@ with a custom drawable that should not be tinted, set
137
322
/>
138
323
```
139
324
140
- ### Thumb attributes
325
+ ### Thumb attributes (deprecated)
141
326
142
327
Element | Attribute | Related method(s) | Default value
143
328
------------- | --------------- | ----------------------------------------- | -------------
144
329
** Thumb** | ` android:thumb ` | ` setThumbDrawable ` <br />` getThumbDrawable ` | inherits from ` SwitchCompat `
145
330
** Color** | ` app:thumbTint ` | ` setThumbTintList ` <br />` getThumbTintList ` | ` ?attr/colorOnSurface ` (unchecked)<br />` ?attr/colorPrimary ` (checked)
146
331
** Elevation** | N/A | N/A | ` 4dp `
147
332
148
- ### Track attributes
333
+ ### Track attributes (deprecated)
149
334
150
335
Element | Attribute | Related method(s) | Default value
151
336
--------- | --------------- | ----------------------------------------- | -------------
152
337
** Track** | ` app:track ` | ` setTrackDrawable ` <br />` getTrackDrawable ` | inherits from ` SwitchCompat `
153
338
** Color** | ` app:trackTint ` | ` setTrackTintList ` <br />` getTrackTintList ` | ` ?attr/colorOutline ` (unchecked)<br />` ?attr/colorPrimaryContainer ` (checked)
154
339
155
- ### Text label attributes
340
+ ### Text label attributes (deprecated)
156
341
157
342
Element | Attribute | Related method(s) | Default value
158
343
-------------- | ------------------------ | ---------------------------------- | -------------
159
344
** Text label** | ` android:text ` | ` setText ` <br />` getText ` | ` null `
160
345
** Color** | ` android:textColor ` | ` setTextColor ` <br />` getTextColors ` | ` ?android:attr/textColorPrimaryDisableOnly `
161
346
** Typography** | ` android:textAppearance ` | ` setTextAppearance ` | ` ?attr/textAppearanceBodyMedium `
162
347
163
- ### Switch states
348
+ ### Switch states (deprecated)
164
349
165
350
Switches can be on or off. Switches have enabled, hover, focused, and pressed
166
351
states.
@@ -171,9 +356,9 @@ interaction may obstruct the element completely.
171
356
For desktop, the radial reaction isn't needed.
172
357
173
358
![ Switch states in an array. Columns are enabled, disabled, hover, focused,
174
- pressed. Rows are on or off] ( assets/switch/switch_states .png )
359
+ pressed. Rows are on or off] ( assets/switch/switch_states_deprecated .png )
175
360
176
- ### Styles
361
+ ### Styles (deprecated)
177
362
178
363
Element | Style
179
364
----------------- | ----------------------------------------
@@ -186,13 +371,13 @@ See the full list of
186
371
and
187
372
[ attrs] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/res/values/attrs.xml ) .
188
373
189
- ## Theming switches
374
+ ## Theming switches (deprecated)
190
375
191
376
Switches support
192
377
[ Material Theming] ( https://material.io/components/selection-controls#theming ) ,
193
378
which can customize color and typography.
194
379
195
- ### Switch theming example
380
+ ### Switch theming example (deprecated)
196
381
197
382
API and source code:
198
383
@@ -202,9 +387,9 @@ API and source code:
202
387
203
388
The following example shows a list of switches with Material Theming.
204
389
205
- ![ "5 switches with brown text: first switch is on and has pink thumb and track"] ( assets/switch/switch_theming .png )
390
+ ![ "5 switches with brown text: first switch is on and has pink thumb and track"] ( assets/switch/switch_theming_deprecated .png )
206
391
207
- #### Implementing switch theming
392
+ #### Implementing switch theming (deprecated)
208
393
209
394
Use theme attributes in ` res/values/styles.xml ` , which applies to all switches
210
395
and affects other components:
0 commit comments