I\'m trying to use a SwitchPreference in my Settings page, but i can\'t get the listener to work. I am using a custom widget layout in order to style the switch, as I cannot
The id of SwitchView should be @android:id/switch_widget
instead of @+id/custom_switch_item
or any other custom id.
In the source of SwitchPreference
class, we can find the code:
View switchView = view.findViewById(AndroidResources.ANDROID_R_SWITCH_WIDGET);
and in AndroidResources
:
static final int ANDROID_R_SWITCH_WIDGET = android.R.id.switch_widget;
so only id = @android:id/switch_widget
can be found correctly.
Here's a demo about SwitchPreference's widgetLayout: