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 find how to use themes to achieve this. In my settings.xml i have the following:
<SwitchPreference
android:key="uitestmode"
android:summary="Enable to display test data in fragments"
android:title="UI Test Mode"
android:widgetLayout="@layout/widget_custom_switch_on_off" >
</SwitchPreference>
and my SettingsFragment looks like:
SwitchPreference uiTestModePref = (SwitchPreference) findPreference("uitestmode");
uiTestModePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(preference.getKey(), newValue.toString());
return false;
}
});
and my custom layout for my switch:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_switch_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textIsSelectable="false"
android:textSize="18sp"
android:textOn="On"
android:textOff="Off"
android:textStyle="bold"
android:thumb="@drawable/carcast_switch_inner_holo_dark"
android:track="@drawable/carcast_switch_track_holo_dark" />
The problem I have is that when I click the switch, the listener is not called. Does anybody know why and how to fix it?