1

I am using SwitchPreference but it seems that only clicking on the preference view(row) triggers either preference click listener or preference change listener. After clicking the switch itself the listeners don't get called even though the switch state changes.

Is there anything to be done about this?

EDIT: I figured something out. This is my listener:

    switchPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            //not called when clicking preference itself
            return true;
        }
    });

And this is my SwitchPreference:

<SwitchPreference
        android:key="@string/key_email_messages"
        android:layout="@layout/prefs_switch"
        android:title="@string/title_email_messages" />

I am using a custom layout to change font size of the preference.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewWrap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/prefs_row_padding">
<TextView
    android:id="@android:id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/pref_text_size" />
<Switch
    android:id="@+id/switch_widget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

I'm guessing SwitchPreference doesn't find the switch with that id. What id I'm supposed to use?

  • You'll need to post your code if you want to get answered, where you attaching the listener and the xml will be helpful too – ronginat Apr 14 '19 at 11:34
  • @ronginat I provided some additional info. – Jonas Kazlauskas Apr 14 '19 at 11:46
  • You're using a custom layout for your SwitchPreference, it's more difficult to achieve. See [this](https://stackoverflow.com/questions/24655834/switchpreference-listener-with-custom-layout) question. – ronginat Apr 14 '19 at 12:22

0 Answers0