ANDROID Preferences List Preference change values dynamically
-
19-06-2021 - |
Question
I am trying to change the entryvalue of a listpreference depending on wether a checkbox is ticked or not...
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);{
if(sharedPrefs.getBoolean("french", false))
{
Preference newsPref = (Preference) findPreference("news_feed");
newsPref.setDefaultValue("@array/newsfeedfr");
}
else
{
Preference newsPref = (Preference) findPreference("news_feed");
newsPref.setEntryValues("@array/newsfeed");
}
}
i get errors on the .setEntryValue with this:
The method setEntryValues(String) is undefined for the type Preference
Solution
One issue that you have is in how you're referencing your array values. @array/newsfeed
is used in XML files, not Java!
You probably want to use getResources().getStringArray(R.array.newsfeed)
or similar. Source.
Another issue is that setEntryValues()
isn't defined for Preferences
, you need to initialise as a ListPreference
instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow