11 SwitchPreference - michael-rapp/AndroidMaterialPreferences GitHub Wiki

The class SwitchPreference is an alternative implementation of the Android SDK's built-in preference SwitchPreference, which is designed according to the Material design guidelines even on pre-Lollipop devices. It provides a toggleable option using a switch widget. Because no dialog is opened when clicking the preference, the getter- and setter-methods, respectively XML attributes, which have been mentioned in section "Common properties" of this documentation, are not supported by this preference.

11.1 Getter and setter methods

The following getter and setter methods are provided by a SwitchPreference.

Getter and setter methods Description Default value Restrictions
getSummaryOn():CharSequence setSummaryOn(CharSequence):void setSummaryOn(int):void Returns or sets the summary, which is shown when the preference is checked. null If the summary is specified as a resource ID, the ID must correspond to a valid string resource.
getSummaryOff():CharSequence setSummaryOff(CharSequence):void setSummaryOff(int):void Returns or sets the summary, which is shown when the preference is not checked. null If the summary is specified as a resource ID, the ID must correspond to a valid string resource.
getDisableDependentsState():boolean setDisableDependentsState(boolean):void Returns or sets, whether dependent preferences are disabled when the preference is checked, or when it is not checked. false none
getSwitchTextOn():CharSequence setSwitchTextOn(CharSequence):void setSwitchTextOn(int):void Returns or sets the text, which is displayed on the preference's switch when it is checked. null If the text is specified as a resource ID, the ID must correspond to a valid string resource.
getSwitchTextOff():CharSequence setSwitchTextOff(CharSequence):void setSwitchTextOff(int):void Returns or sets the text, which is displayed on the preference's switch when it is not checked. null If the text is specified as a resource ID, the ID must correspond to a valid string resource.
isChecked():boolean setChecked(boolean):void Returns or sets, whether the preference is checked, or not. If a value is set, it is instantly persisted. false none

11.2 XML attributes

Instead of previously shown getter and setter methods, the following XML attributes can be used in order to specify the properties of a SwitchPreference.

XML attribute Description Format
android:summaryOn The summary, which is shown when the preference is checked. string
android:summaryOff The summary, which is shown when the preference is not checked. string
android:disableDependentsState Whether dependent preferences should be disabled when the preference is checked, or when it is not checked. boolean
android:switchTextOn The text, which is displayed on the preference's switch when the preference is checked. string
android:switchTextOff The text, which is displayed on the preference's switch when the preference is not checked. string
android:defaultValue The default value of the preference. boolean