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.
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 have ListPreference for selecting languages. The current trnalsations are defined in their own strings.xml as seen in the image. To fill the preference list i include the string-array elements inside the main strings.xml, making it a static configuration.
Question: Is there any way to make the Language ListPreference dynamic by iterating through all the strings.xml and extracting the Locale information from them some how on run time?
This is the current static setup:
values/strings/strings.xml
<resources>
<!-- Preferences -->
<string name="pref_language">Language</string>
<string-array name="pref_language_list">
<item>English</item>
<item>Svenska</item>
<item>Deutsch</item>
<item>Français</item>
<item>Português</item>
<item>Español</item>
<item>Pусский</item>
</string-array>
<string-array name="pref_language_values">
<item>en</item>
<item>se</item>
<item>de</item>
<item>fr</item>
<item>pt</item>
<item>es</item>
<item>ba</item>
</string-array>
</resources>
xml/pref_general.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:entries="#array/pref_language_list"
android:entryValues="#array/pref_language_values"
android:key="language"
android:title="#string/pref_language"
android:icon="#drawable/language"
/>
</PreferenceScreen>
entries and entryValues should work.
while I don't understand these useless string definitions & string assignments ...
add them directly into the arrays; into values/arrays.xml; with attribute translatable="false".
and these language tags also don't match the resource file locations.
<resources>
<string-array name="pref_language_list" translatable="false">
<item>English</item>
<item>Svenska</item>
<item>Deutsch</item>
<item>Français</item>
<item>Português</item>
<item>Español</item>
<item>Pусский</item>
</string-array>
<string-array name="pref_language_values" translatable="false">
<item>en</item>
<item>se-rSE</item>
<item>de-rDE</item>
<item>fr-rFR</item>
<item>pt-rPT</item>
<item>es-rES</item>
<item>ba-rRU</item>
</string-array>
</resources>
When I run the app for the first time, the value of dateFormat is empty,
I'm very surprised, the dateFormat should return default vale"HH:mm dd-MM-yyyy"
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String dateFormat=dateFormat("SetDate", "HH:mm dd-MM-yyyy");
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="AppPreference"
android:summary="#string/Preferencesummary"
android:title="#string/Preference" >
<ListPreference
android:defaultValue="\n"
android:dialogTitle="#string/DateFormat"
android:entries="#array/Mydate"
android:entryValues="#array/Mydate_values"
android:key="SetDate"
android:summary="#string/DateFormatsummary"
android:title="#string/DateFormat"
android:layout="#layout/mypreference_layout"
/>
</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Mydate">
<item>HH:mm dd-MM-yyyy</item>
<item>HH:mm MM-dd-yyyy</item>
<item>yyyy-MM-dd HH:mm</item>
</string-array>
<string-array name="Mydate_values">
<item>HH:mm dd-MM-yyyy</item>
<item>HH:mm MM-dd-yyyy</item>
<item>yyyy-MM-dd HH:mm</item>
</string-array>
</resources>
Well this looks like the problem to me:
android:defaultValue="\n"
Why would you expect a default value of "HH:mm dd-MM-yyyy" when you're explicitly setting it to "\n"? I suggest you change the default value, and see if that works...
(I'm not an Android developer, so maybe I'm missing something... but that seems like the obvious approach to me.)
I want to get the value of the selected item in the spinner. I am using array adapter.
<string-array name="my_list">
<item value="">---Select the value from the List---</item>
<item value="value1">data1</item>
<item value="value2">data2</item>
<item value="value3">data3</item>
<item value="value4">data4</item>
<item value="value5">data5</item>
</string-array>
If I select the "data1" in my spinner, I want to get the "value1"..not "data1"
Anyone help me. Quick response helps me a lot. thanks in advance.
You have to add a values string-array like this instead of value attribute
<string-array name="my_list">
<item value="">---Select the value from the List---</item>
<item>data1</item>
<item>data2</item>
<item>data3</item>
<item>data4</item>
<item>data5</item>
</string-array>
<string-array name="my_list_values">
<item value="">---Select the value from the List---</item>
<item>value1</item>
<item>value2</item>
<item>value3</item>
<item>value4</item>
<item>value5</item>
</string-array>
To retrieve the values from the my_list_values, You need to write this in onItemSelected function in listener
String selectedValue = getResources().getStringArray(R.array.my_list_values)[parent.getSelectedItemPosition()];
You cann't do something like you have done directly as Android doesn't supports entryValues
I don't think that's possible, tried it myself a while back.
Maybe you just do what I did and create a second array holding the values you want on the same index?
I have a set of preferences stored in arrays.xml, which looks similar to this:
<string-array name="firstname">
<item name="1">Bob</item>
<item name="6">Kevin</item>
<item name="3">Peter</item>
<item name="4">Paul</item>
<item name="5">Simon</item>
<item name="2">Matt</item>
</string-array>
<string-array name="FirstnameValues">
<item name="1">1</item>
<item name="2">2</item>
<item name="3">3</item>
<item name="4">4</item>
<item name="5">5</item>
<item name="6">6</item>
</string-array>
These are used to populate ListPreference. The item stored in SharedPreferences is the name of the item; for example, for Kevin the items stored in SharedPreferences would be 6.
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String Firstname = prefs.getString("namesList", "");
So here, Firstname would give me 6, when selecting the SharedPreference key namesList for the selected item Kevin.
All I want to do is read the array.xml, so I can relate the number 6 back to Kevin. I can use getResources().getStringArray(R.array.testArray); but this just returns an array containing "Bob, Kevin, Peter, Paul, Simon, Matt."
It does not include the named value.
Do you have any idea on how I can get the name as well as the item for each?
Well, I don't know if you can do it directly, but you could always create a wrapper class that loads both string arrays and gives you both values for any given array value.
When you define you preferences in your settings.xml you can access the arrays.xml as follows (applogies for typos)
<ListPreference
android:title="FirstNameValues"
android:summary="First Names"
android:key="fistNamePreference"
android:defaultValue="1"
android:entries="#array/firstName"
android:entryValues="#array/fistNameValues" />
In your activity onCreate add
addPreferencesFromResource(R.xml.settings);
If you need more clarification please let me know. Here is an example.
Arrays.xml
<string-array name="timeIntervalList">
<item>5 seconds</item>
<item>10 seconds</item>
<item>30 seconds</item>
<item>1 minute</item>
<item>5 minutes</item>
<item>10 minutes</item>
</string-array>
<string-array name="timeIntevalValues">
<item>5000</item>
<item>10000</item>
<item>30000</item>
<item>60000</item>
<item>300000</item>
<item>600000</item>
</string-array>
settings.xml
<ListPreference
android:title="Retry Interval"
android:summary="Time interval"
android:key="retryPeriodPref"
android:defaultValue="5000"
android:entries="#array/timeIntervalList"
android:entryValues="#array/timeIntevalValues" />
settingsAcitivty
in onCreate call
addPreferencesFromResource(R.xml.settings);
OtherAcitivity where you wish to use the preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
timerPeriod = Integer.parseInt(prefs.getString("retryPeriodPref", "5000"));