@@ -31,7 +31,7 @@ Material Components for Android library. For more information, go to the
3131[ Getting started] ( https://github.com/material-components/material-components-android/tree/master/docs/getting-started.md )
3232page.
3333
34- ** Note:** The ` SwitchMaterial ` widget provides a complete implementation of
34+ ** Note:** The ` MaterialSwitch ` widget provides a complete implementation of
3535Material Design's switch component. It extends from the support library's
3636` SwitchCompat ` widget, but not from the framework ` Switch ` widget. As such, it
3737does 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
5656switch consists of a track and thumb; the thumb moves along the track to
5757indicate its current state.
5858
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+
5969### Switches example
6070
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+
61246API and source code:
62247
63248* ` SwitchMaterial `
@@ -66,7 +251,7 @@ API and source code:
66251
67252The following example shows a list of five switches.
68253
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 )
70255
71256In the layout:
72257
@@ -99,25 +284,25 @@ In code:
99284
100285``` kt
101286// To check a switch
102- switchMaterial .isChecked = true
287+ switchmaterial .isChecked = true
103288
104289// To listen for a switch's checked/unchecked state changes
105- switchMaterial .setOnCheckedChangeListener { buttonView, isChecked
290+ switchmaterial .setOnCheckedChangeListener { buttonView, isChecked
106291 // Responds to switch being checked/unchecked
107292}
108293```
109294
110- ## Anatomy and key properties
295+ ## Anatomy and key properties (deprecated)
111296
112297The following is an anatomy diagram that shows a switch thumb and a switch
113298track:
114299
115- ![ Switch anatomy diagram] ( assets/switch/switch_anatomy .png )
300+ ![ Switch anatomy diagram] ( assets/switch/switch_anatomy_deprecated .png )
116301
1173021 . Thumb
1183032 . Track
119304
120- ### Switch attributes
305+ ### Switch attributes (deprecated)
121306
122307Element | Attribute | Related method(s) | Default value
123308-------------------------- | ------------------------------------------ | ---------------------------------------------------------- | -------------
@@ -137,30 +322,30 @@ with a custom drawable that should not be tinted, set
137322 />
138323```
139324
140- ### Thumb attributes
325+ ### Thumb attributes (deprecated)
141326
142327Element | Attribute | Related method(s) | Default value
143328------------- | --------------- | ----------------------------------------- | -------------
144329** Thumb** | ` android:thumb ` | ` setThumbDrawable ` <br />` getThumbDrawable ` | inherits from ` SwitchCompat `
145330** Color** | ` app:thumbTint ` | ` setThumbTintList ` <br />` getThumbTintList ` | ` ?attr/colorOnSurface ` (unchecked)<br />` ?attr/colorPrimary ` (checked)
146331** Elevation** | N/A | N/A | ` 4dp `
147332
148- ### Track attributes
333+ ### Track attributes (deprecated)
149334
150335Element | Attribute | Related method(s) | Default value
151336--------- | --------------- | ----------------------------------------- | -------------
152337** Track** | ` app:track ` | ` setTrackDrawable ` <br />` getTrackDrawable ` | inherits from ` SwitchCompat `
153338** Color** | ` app:trackTint ` | ` setTrackTintList ` <br />` getTrackTintList ` | ` ?attr/colorOutline ` (unchecked)<br />` ?attr/colorPrimaryContainer ` (checked)
154339
155- ### Text label attributes
340+ ### Text label attributes (deprecated)
156341
157342Element | Attribute | Related method(s) | Default value
158343-------------- | ------------------------ | ---------------------------------- | -------------
159344** Text label** | ` android:text ` | ` setText ` <br />` getText ` | ` null `
160345** Color** | ` android:textColor ` | ` setTextColor ` <br />` getTextColors ` | ` ?android:attr/textColorPrimaryDisableOnly `
161346** Typography** | ` android:textAppearance ` | ` setTextAppearance ` | ` ?attr/textAppearanceBodyMedium `
162347
163- ### Switch states
348+ ### Switch states (deprecated)
164349
165350Switches can be on or off. Switches have enabled, hover, focused, and pressed
166351states.
@@ -171,9 +356,9 @@ interaction may obstruct the element completely.
171356For desktop, the radial reaction isn't needed.
172357
173358![ 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 )
175360
176- ### Styles
361+ ### Styles (deprecated)
177362
178363Element | Style
179364----------------- | ----------------------------------------
@@ -186,13 +371,13 @@ See the full list of
186371and
187372[ attrs] ( https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/switchmaterial/res/values/attrs.xml ) .
188373
189- ## Theming switches
374+ ## Theming switches (deprecated)
190375
191376Switches support
192377[ Material Theming] ( https://material.io/components/selection-controls#theming ) ,
193378which can customize color and typography.
194379
195- ### Switch theming example
380+ ### Switch theming example (deprecated)
196381
197382API and source code:
198383
@@ -202,9 +387,9 @@ API and source code:
202387
203388The following example shows a list of switches with Material Theming.
204389
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 )
206391
207- #### Implementing switch theming
392+ #### Implementing switch theming (deprecated)
208393
209394Use theme attributes in ` res/values/styles.xml ` , which applies to all switches
210395and affects other components:
0 commit comments