3

I'm working with my PreferenceActivity where I have a SwitchPreference to which I didn't assing a title using android:title="Title" because I'd like to change it whenever the Switch is clicked.

I've tried this way but didn't work. I also searched on SO related topics without solving my problem.

public class PrefsFragment extends PreferenceFragment {

    private SwitchPreference pref;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
        // Load the preferences from an XML resource

        pref = (SwitchPreference)findPreference("SWITCH");
        pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if (newValue.toString().equals("SWITCH")) {
                    boolean test = (Boolean) newValue;
                    if (test) {
                        pref.setTitle("ChangedTitle");
                    } else {
                        pref.setTitle("DefaultTitle");
                    }
                }

                return true;
            }
        });
    }
}

settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="Settings">

        <ListPreference
            android:key="yourPref"
            android:title="Title"
            android:dialogTitle="Select"
            android:summary="%s"
            android:defaultValue="4"
            android:entries="@array/titles"
            android:entryValues="@array/values"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="Title">

        <SwitchPreference
            android:key="SWITCH"/>

    </PreferenceCategory>
</PreferenceScreen>

Could you help me please?

Pier
  • 794
  • 1
  • 12
  • 27

2 Answers2

0

Try calling preference.setTitle("YOUR TEXT"); within the listener callback, rather than pref.setTitle"YOUR TEXT"); The listener is passing in the preference that was changed.

M. Palsich
  • 1,748
  • 13
  • 18
  • No changes. I also posted the xml file – Pier Aug 25 '16 at 19:52
  • You're sure you want to call `setTitle()` and not `setSummary()`? – M. Palsich Aug 25 '16 at 19:54
  • Yes, I'd like _setTitle()_ more. But just to know... even with _setSummary()_ I don't get changes – Pier Aug 25 '16 at 19:57
  • `newValue` is described as "the new value of the preference" in the Android documentation. Are you sure this is the correct check to be making? `''SWITCH"` is the key of your preference, not the value. Check out this link about defining preferences in XML https://developer.android.com/guide/topics/ui/settings.html#DefiningPrefs – M. Palsich Aug 25 '16 at 19:58
  • So how could achieve that? – Pier Aug 25 '16 at 20:11
  • What are the values in your string array at `@arrays/values`? These are the results you'll want to check against when determining which value to assign to the title. I'm assuming your if-else block is never actually reached because the conditional is always going to be false unless `"SWITCH"` is one of the values the switch can take on. – M. Palsich Aug 25 '16 at 20:43
  • Sorry, I'm losing your train of thought. Why should I care about _@arrays/values_ if I'm working with SwitchPreference? Those are part of ListPreference – Pier Aug 25 '16 at 20:48
  • What I'm trying to say is your switch has no values. You cannot be checking values if you have no values. Currently you are checking the new value of your switch against the key of your switch. Give you switch some values, and do your check against those values, not the key. – M. Palsich Aug 25 '16 at 20:51
  • Could you enlighten me? Then I'll accept the answer – Pier Aug 25 '16 at 20:59
0

The problem is with your OnSharedPreferenceChangeListener. Instead of this,

pref = (SwitchPreference)findPreference("SWITCH");
        pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if (newValue.toString().equals("SWITCH")) {
                    boolean test = (Boolean) newValue;
                    if (test) {
                        pref.setTitle("ChangedTitle");
                    } else {
                        pref.setTitle("DefaultTitle");
                    }
                }

                return true;
            }
        });

Try this

SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        if (s.equals("SWITCH")) {
            SwitchPreference sp = (SwitchPreference) findPreference(s);
            boolean test = sharedPreferences.getBoolean(s, false);
            if (test) { 
                sp.setTitle("ChangedTitle"); 
            } else { 
                sp.setTitle("DefaultTitle");
            }
        }
    }
}
    
Harsh Modani
  • 205
  • 1
  • 11