Android - Reading arrays.xml - android

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"));

Related

How to add all predefined languages into a ListPreference dynamically?

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>

Reading application preferences as Integer in Android

I am developing a sample application where I have "ListPreference" like this:
<ListPreference
android:defaultValue="180"
android:entries="#array/pref_sync_frequency_titles"
android:entryValues="#array/pref_sync_frequency_values"
android:key="sync_frequency"
android:negativeButtonText="#null"
android:positiveButtonText="#null"
android:title="#string/pref_title_sync_frequency"/>
and I bind it to the array of integers like this:
<string name="pref_title_sync_frequency">Sync frequency</string>
<string-array name="pref_sync_frequency_titles">
<item>15 minutes</item>
<item>30 minutes</item>
<item>1 hour</item>
<item>3 hours</item>
<item>6 hours</item>
<item>12 hours</item>
<item>24 hours</item>
<item>Never</item>
</string-array>
<integer-array name="pref_sync_frequency_values">
<item>15</item>
<item>30</item>
<item>60</item>
<item>180</item>
<item>360</item>
<item>720</item>
<item>1440</item>
<item>-1</item>
</integer-array>
From what I understand the persistence of the preference is provided by the PreferenceFragment from which I extend the fragment where I have the ListPreference. I don't have any extra code except for what was provided when I was creating a Setting activity in Android Studio:
public static class DataSyncPreferenceFragment extends PreferenceFragment
Now the problem is that when I try to read the preference, I want to read it as integers like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
return sharedPref.getInt("sync_frequency", 0);
But I get this exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
I can read the preference as a sting and then convert to int, but I would expect the preference already stored as an Integer since I bind it to an array of integers. Why is it stored as a String and is it possible to store it as an Integer?

how to extract id attribute value from string-array

I use below code in my array.xml file inorder to defining a array:
<string-array name="cities_azarbayejanSharghi">
<item id="15">A</item>
<item id="36">B</item>
<item id="140">C</item>
<item id="95">D</item>
</string-array>
I want to access the id of each field in the code when it is selected as spinner's selecteditem. Is there any ways?
TypedArray ta = getResources().obtainTypedArray(R.array.your_array);
int resId = ta.getResourceId(arg_positionItem, 0);
You could be better off referencing other strings, such as the answer outlined here:
<string name="15">A</string>
<string name="36">B</string>
<string-array name="cities_azarbayejanSharghi">
<item>#string/15</item>
<item>#string/36</item>
</string-array>

There is no Options on my list

Im trying to set up a preferences but there is no options on the list here is the code
<ListPreference
android:title="list"
android:key="list"
android:summary= "This is a list to choose from"
android:entries="#array/list"
android:entryValues="#array/lvalues"
/>
Here is the code for the array
<string-array name="list">
<item Option="1"></item>
<item Option="2"></item>
<item Option="3"></item>
<item Option="4"></item>
</string-array>
<string-array name="lvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
I want it to say Option 1 etc next to the thing
Is this what you where trying to do?
<string-array name="list">
<item>Option 1</item>
<item>Option 2</item>
<item>Option 3</item>
<item>Option 4</item>
</string-array>
<string-array name="lvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
String Array
An array of strings that can be referenced from the application.
Note: A string array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine string array resources with other simple resources in the one XML file, under one <resources> element.
ELEMENTS
<string-array>
Defines an array of strings. Contains one or more elements.
attributes:
name
String. A name for the array. This name will be used as the resource ID to reference the array.
<item>
A string, which can include styling tags. The value can be a reference to another string resource. Must be a child of a <string-array> element. Beware that you must escape apostrophes and quotation marks.
No attributes.
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
Reference/Source:
http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

Get item value in arrayadapter

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?

Categories

Resources