I feel like i have been searching every where without finding an answer to this.
I have a list preference
<ListPreference
android:key="signedUpCompetetion"
android:entries="#array/listArray"
android:entryValues="#array/listValues"
/>
With the following entries and entryValues:
<resources>
<string-array name="listArray">
<item></item>
<item></item>
<item></item>
</string-array>
<string-array name="listValues">
<item></item>
<item></item>
<item></item>
</string-array>
</resources>
So say that in one of my activities i want to add something to the list.
The following is an attempt but it doesnt seem to work:
this.appPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
this.prefEditor = appPrefs.edit();
ListPreference list = (ListPreference) getSharedPreferences("signedUpCompetetion", MODE_PRIVATE);
list.setEntries("This is a test");
Anyone able to tell me how to add to a listpreference?
Update
By the looks of it this seems to work
PreferenceScreen root = this.getPreferenceScreen();
ListPreference list = (ListPreference) root.findPreference("signedUpCompetetion");
CharSequence[] entries = { "One", "Two", "Three" };
CharSequence[] entryValues = { "1", "2", "3" };
list.setEntries(entries);
list.setEntryValues(entryValues);
However there is 1 problem! if i restart the application the list is not preserved. which means that the list is empty!
Use setEntries(int) and setEntryValues(int)
list.setEntries(R.array.listArray);
list.setEntryValues(R.array.listValues);
Update for how to persist the data:
You can persist the data in several ways, the simplest one would be to call setPersistent(true):
list.setPersistent(true);
Related
I want to use a predefined list of elements in the android app settings using a MultiSelectListPreference and a few values already predifined in an xml file.
This is how it looks like
XML-File
<resources>
<string-array name="myTestList">
<item>Item 1</item>
<item>Item 2</item>
</string-array>
</resources>
And in the preferences.xml I added
<PreferenceCategory app:title="Example Title">
<MultiSelectListPreference
android:entries="#array/myTestList"
android:entryValues="#array/myTestList"
android:summary="yes / no"
android:title="Example Title 2" />
</PreferenceCategory>
So far it works and with
val myPreference: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
var sets: MutableSet<String?> = HashSet()
sets = myPreference.getStringSet("myTestList", HashSet<String>()) as MutableSet<String?>
sets.add("Item 3")
myPreference.edit().putStringSet("myTestList", sets).apply()
System.out.println("New list" + myPreference.getStringSet("myTestList", HashSet<String>()) as MutableSet<String?>)
What it does is to take the values from the predefined xml, adds a new element, save it and it works fine when i do the system out.
But now my problem: this new list should now be displayed in the settings of the app and here I fail to understand what i have to do to display it instead of using
android:entries="#array/myTestList"
android:entryValues="#array/myTestList"
Which ofcause is not working as this just points to the xml which now got updated.
Hope its understandable what i mean - im a beginner and using this forum for the first time.
Thanks for your help
I am trying to implement android jetpack preference for my settings screen.
everything is working good, when I click on MultiSelectListPreference, it shows the list of entries, but I have few questions,
why can't the entryValues be a integer-array? (string-array is working fine)
how to set default values? for eg: I want to set the second and third entry to be checked by default in the beginning.
here is a part of my pref.xml file
...
app:entries="#array/res_entries"
app:entryValues="#array/res_id_values"
app:defaultValue="#array/res_def_values" //this line is not working
...
If I set res_id_values in arrays.xml file to be a integer-array, then app is crashing.
My settingsFragment class extends PreferenceFragmentCompat and override onCreatePreferences and in it, I have written
setPreferencesFromResource(R.xml.pref.xml, rootkey)
EDIT
My res_entries array :
<string-array name="res_entries">
<item>apple</item>
<item>Mango</item>
<item>Guava</item>
</string-array>
My res_id_values array :
<string-array name="res_id_values">
<item>1</item>
<item>2</item>
<item>12</item>
</string-array>
My res_def_values array :
<string-array name="res_def_values">
<item>true</item>
<item>false</item>
<item>true</item>
</string-array>
So finally, I solved it.
All thanks to #CommonsWare .
answer to my first question is that, we cannot use integer-array. Use a string array and store integer values in it. later when you get it, use Integer.parseInt().
now coming to second question, to store default value(let's say you want second and third item to be checked by default), use res_id_values in res_def_values. Don't use true/false or 0/1 like me.
eg: if in above question, If I want apple and guava to be checked by default, then my res_def_values will look like this:
<string-array name="res_def_values">
<item>1</item>
<item>12</item>
</string-array>
I have a settings menu with a list preference from the sample settings activity in Android Studio.
<ListPreference
android:key="example_list"
android:title="#string/pref_title_add_friends_to_messages"
android:defaultValue="5"
android:entries="#array/pref_example_list_titles"
android:entryValues="#array/pref_example_list_values"
android:negativeButtonText="#null"
android:positiveButtonText="#null" />
In this list preference you can select 8 different things.
<string name="pref_title_add_friends_to_messages">Klasse</string>
<string-array name="pref_example_list_titles">
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>
<string-array name="pref_example_list_values">
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>
I want to use the value of the preference to display links which belong to the 8 different settings.
E.g.:
5 - google.com
6 - wikipedia.com
etc.
As a beginner, how do I get the value of my preference and how do assign the values to the links and put them in one variable, which changes when the preference is being changed?
<string-array name="pref_example_list_values"> will be used as value in the ListPreference. To get the current value, use:
ListPreference lp = (ListPreference) findPreference("example_list");
String currentValue = lp.getValue();
To get the value and display it in a text, have a TextView and set the text:
/* You might create 'if' statement for 8 times because of there are 8 different value in the ListPreference*/
TextView tv = (TextView) findViewById(R.id.textview1);
if (currentValue.equals("5")) {
// do your thing here, i.e. google.com
tv.setText("Welcome, 5!");
}
if (currentValue.equals("6")) {
// do your thing here, i.e. wikipedia.com
tv.setText("Welcome, 6!");
}
You can get the value of your preference like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String value = sharedPref.getString("example_list", "default value");
I don't understand the rest of your question. What does
how do assign the values to the links and put them in one variable, which changes when the preference is being changed?
mean?
Also, I find the documentation on preference settings very helpful.
I am looking for a guide as to how i can implement switch that would get values from a string array. Basically, i have a spinner with all the items as stated in my array, so i want to implement a switch so that whenever i click on an item, it will trigger an event, for example, going to another activity.
<string-array name="location1">
<item>string a</item>
<item>string a</item>
<item>string b</item>
<item>string c</item>
<item>string d</item>
</string-array>
So, if i have a string array like this, how should I implement my switch statement?
If array is:
<string-array name="cars_array">
<item>Audi</item>
<item>Ferrari</item>
</string-array>
You can access them like this:
Resources res = getResources();
String[] cars = res.getStringArray(R.array.cars_array);
then you can access them individually as cars[0], cars[1] etc
If you want to access the same string resource as part of an array and individually as well, then you can do this:
<string name="audi">Audi</string>
<string name="ferrari">Ferrari</string>
<string-array name="cars_array">
<item>#string/audi</item>
<item>#string/ferrari</item>
</string-array>
Then you can simply say "#string/audi" to get it individually as well as you can use the array name "#string/cars" to use the string array as a whole.
I think this resource on Android Developers site might help you : String Resources
It will get you something like that :
Resources res = getResources();
String[] strArray = res.getStringArray(R.array.location1);
I have a List Preference for Accelerometer mode in my preference activity which I'm loading purely from xml. If a users device does not have or support an Accelerometer I wont to completely remove the item in the array. Do I have to create a separate list preference for this one option to remove it? (I would prefer to keep them all in one.) How can it be removed with the below setup?
Thanks for the tip.
Jason
<string-array name="mode_text">
<item>Others Modes</item>
<item>Others Modes</item>
<item>Accelerometer Mode</item>
</string-array>
<string-array name="mode_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
When you say you want to "completely remove the item in the array", do you mean you want the ListPreference to not show in the PreferenceScreen? Or you want it to return a null value when you try to get the set value?
Okay, I understand now. I think you can't remove just that item in the array. You have to set new arrays for the entries and entryValues for your ListPreference, programatically.
You find the ListPreference by its key.
Here is an example:
if (accelerometerNotSupported) {
ListPreference accelMode = (ListPreference) findPreference("acceleratorMode");
accelMode.setEntries(new String[]{"Others Modes","Others Modes"});
accelMode.setEntryValues(new String[]{"0", "1"});
}
That will change your list preference to have only those two options.