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?
Related
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>
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.
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
I know how to use strings.xml for global strings in Android, but what's about the String[] ?
Is there any possibility to use this and if there is, how?
Thank you.
If you meant String arrays inside and xml refer here
Create a file in your values folder called arrays.xml and create your array like this
<string-array name="colors">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
<item>violet</item>
</string-array>
Not Directly in strings.xml, but something like this can be done in string.xml:
<string name="string1">str1</string>
<string name="string2">str2</string>
<string-array name="system">
<item>#string/string1</item>
<item>#string/string2</item>
</string-array>
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"));