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
Related
I am using Android studio and have an array in my string.xml file as:
<string-array name="my_array">
<item>text1</item>
<item>text2</item>
<item>text3</item>
</string-array>
I know how to access the array (and get the 1st item) in my MainActivity.java file:
myButton.setText(getResources().getStringArray(R.array.my_array)[0]);
My question: Is there anyway to set the text directly in the activity_main.xml file? I tried:
<Button
android:id="#+id/myButton"
android:text="#array/my_array[0]"
... />
but that causes an error. Without the "[0]" it displays the 1st value (text1), but maybe that is just because of the button's size and it's not showing the rest - I can't get it to display other items (e.g., text2).
Is it possible to access one value of the array directly in the layout file?
Thanks.
I found a good answer here: https://stackoverflow.com/a/4161645/933969
Basically you create named strings first (and use those where you would want mystrings[x]) and then create your array using references to those named strings:
<string name="earth">Earth</string>
<string name="moon">Moon</string>
<string-array name="system">
<item>#string/earth</item>
<item>#string/moon</item>
</string-array>
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>
How can i create a simple radio button preferences to choose which led color use in BATTERY_LOW condition?
To change the led color:
Check this link
For the BATTERY_LOW you will ned a broadcastreceiver:
Check this link
Ok.
First, create a file in the values folder. Call it something like "preference_res.xml". Then add the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="prefColor">
<item>None</item>
<item>Blue</item>
<item>Green</item>
<item>Yellow</item>
<item>Other Color</item>
</string-array>
<string-array
name="prefColorVal">
<item>-1</item>
<item>-2</item>
<item>79</item>
<item>24</item>
<item>25</item>
<item>27</item>
<item>80</item>
</string-array>
</resources>
(Be aware that the prefColorVal items should ref to the value you want the radio button to have. The values shown here are random.)
In your preference.xml, add something like this:
<ListPreference
android:dialogTitle="Choose color"
android:entries="#array/prefColor"
android:entryValues="#array/prefColorVal"
android:key="bat_below_15"
android:negativeButtonText="Cancel"
android:positiveButtonText="Save"
android:summary="Choose color when below 15"
android:title="Color when below 15" />
If you don't know how to get or use these values, here are some pointers, but you should google this stuff:
preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
and use this to get the value:
preferences.getString("bat_below_15", Blue));
Hope it helps.
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);
I am using a spinner populating with an xml file:
<string-array name="Spinner Items">
<item>Item 1: 2.0 - 4.0</item>
<item>Item 2: 1.0 - 3.0</item>
Is it possible to set a name value pair like this:
<string-array name="Spinner Items">
<item name="Item 1: 2.0 - 4.0" value="2"></item>
<item name="Item 2: 1.0 - 3.0" value="1.5"></item>
So that I can populate the spinner with the name and then when an item is selected it returns the value of that specific item? I figure it must be easier to do all this in the xml file as opposed to have to write if statement for each item when the spinner is changed. Of course the real xml file is much longer than this snippet.
The only way to do this is to implement a custom adapter-class.
I did it wrong way, but it works.
My Spinner(spinnerSex) contains Male/Female choice:
<string-array name="spinnerSex">
<item>Male</item>
<item>Female</item>
</string-array>
java code:
Object objSelectedItem = spinnerSex.getSelectedItem();
String strSelectedItem = String.valueOf(objSelectedItem);
String[] arrvals = this.getResources().getStringArray(R.array.spinnerValues);
boolean isMale = (strSelectedItem.equals(arrvals[0]));