I am following the documentation for creating my preferences xml, which contains a drop down preference populated with a list of languages. I looked up the documentation, and other questions on Stack Overflow, and I should be able to assign an array of strings for the entryValues property of my preference. However, I keep getting an error that says that a String was expected, not an array.
This is my Preferences xml up until now
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<DropDownPreference
android:defaultValue="true"
android:entries="#array/language_settings_options"
android:entryValues="#array/locale_language_preference"
android:key="language_preference"
android:title="#string/language_settings_title" />
</PreferenceScreen>
What am I doing wrong?
Related
I'm writing preferences page (preferences.xml), and trying to retrieve values from resources file. This code is working;
<PreferenceScreen ...
<ListPreference
android:entries="#array/tips"
android:entryValues="#array/tips"
android:title="Tip rate"
/>
But this is not;
<EditTextPreference
android:defaultValue="#integer/invoiceNumberNext" // this doesn't work
android:key="invoice_number_next"
android:title="Next Invoice #"
android:inputType="numberDecimal"
/>
I have this in my defaults.xml page which is under res/values
<resources>
<integer name="invoiceNumberNext">1001</integer>
</resources>
Any idea why I'm not able to fetch some values?
The problem is that you misplaced your value. If you reference a value with #integer it must be in a xml file called (what a mystery...) integer.xml. This file must be in the folder structure
res/values/integer.xml
So right click on values folder and click "New-->Values resource file" and then enter integer.xml Here, now add your resources like you have done....tataaaa....you can use it.
EDIT
As I hadn´t understand the original question correctly, I have to elaborate this answer. Of course it is possible to create a file defaults.xml and put an <integer> item inside, and this is still available by #integer. So, at the beginning of this question, it seems that you misplaced the file into the wrong directory. But that isn´t the case so your problem must be anywhere else. If the default.xml is in res/values, it should work.
I wanna implement custom sounds in Settings. For this i'm using ListPreference. As we know
android:entries - The human-readable array to present as a list.
android:entryValues - The array to find the value to save for a preference when an entry from entries is selected.
<ListPreference
android:key="sound_income"
android:title="#string/sound_income"
android:entries="#array/sound_income"
android:entryValues="#array/sound_income"
android:summary="Cutting through"/>
<string-array name="sound_income">
<item>Cutting through</item>
<item>Don don</item>
<item>Oringz pack nine</item>
<item>Oringz pack</item>
</string-array>
Right now shared preference saves above texts but in order to play sound i need to get resId like this R.raw.cutting_through. The question is that how can i implement android:entryValues="#array/sound_income_values" string array to create sound`s int value not their names?
I have added a listPref like this in my settings.xml:
<PreferenceCategory
android:title="Presets" >
<ListPreference
android:defaultValue="1"
android:entries="#array/pre_entries"
android:entryValues="#array/pre_values"
android:key="pre_type"
android:summary="Choose between various presets"
android:title="Presets" />
</PreferenceCategory>
Is there any way I can update or add to the entries and entryValues array?
I mean I know I can create a custom pref and then add the values but is there a way around it?
Yes. You can get the entries and their values using listPreference.getEntries() and getEntryValues(). Then add the new value to that array (it may be easiest to create a new array and copy them over if you want to insert into the middle). Then call setEntries and setEntryValues on the new arrays to set the new list.
I have setup ability for a user to specify some settings using the in-built preference system. My preference.xml is simple, with only a ListPreference:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" android:persistent="true">
<PreferenceCategory android:title="Your nuSTOCK Settings" android:persistent="true">
<ListPreference android:key="operation_section" android:entries="#array/array_nustock_section_values" android:summary="What's your operational section in nuSTOCK?" android:entryValues="#array/array_nustock_section_keys" android:title="Operation Section" android:negativeButtonText="Cancel" android:positiveButtonText="OK" android:persistent="true" android:enabled="true"/>
</PreferenceCategory>
</PreferenceScreen>
That references my Arrays, which are:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="array_nustock_section_keys">
<item>store</item>
<item>branch</item>
</string-array>
<string-array name="array_nustock_section_values">
<item>Store</item>
<item>Branch</item>
</string-array>
</resources>
And then I load it (the Preference Module) into my Activity Like this:
nustock_preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
After which I then prompt the user to set the values (select from only two options), by invoking the preference activity via an Intent:
Intent settingsActivity = new Intent(this,
MyPreferenceActivity.class);
startActivity(settingsActivity);
The Preference Activity is like so:
public class MyPreferenceActivity extends PreferenceActivity {
private static final String PREF_FILENAME = "nustock_preferences";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(PREF_FILENAME);
addPreferencesFromResource(R.xml.preferences);
}
}
And I then try to read the preference value set by the user like this:
nustock_preferences = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String op = nustock_preferences.getString(PREF_OPERATION_SECTION,"none");
Log.d(Tag,String.format("Operation Section : %s", op));
PROBLEM:
No matter what value of the preference I select, only value I get is the default "none" (which I've actually added as different from the actual values in the list, just to highlight the problem -- selected value never gets returned!).
So, What Am I doing wrong? I've tried many variations of this approach, but I can't get the user's selected preference! Even tried restarting the app (hoping that the preferences get set at start-up, nothing!)
But, interestingly, whenever I load the preference screen, the correct value is still selected under the ListPreference dialog!
I believe you specify a particular preference file name with getPreferenceManager().setSharedPreferencesName(PREF_FILENAME);
but later you are trying to get preference value from default preferences .getDefaultSharedPreferences(getBaseContext());
It's like writing data to table PERSON, but later trying to find it in the table DEFAULT
Either remove the setting for preference file name, or get your value from the preference file you specified
For my PreferenceActivity, I am using the typical preferences.xml file.
The entryValues of a ListPreference is not something meant to be localized. Is it possible to define the entryValues attribute directly using some kind of array literal?
My first try below won't work, "pull, push" is considered a string:
<ListPreference
android:key="server_reception_mode"
android:title="#string/title_server_reception_mode_preference"
android:entries="#array/entries_server_reception_mode_preference"
android:entryValues="pull, push" />
You can declare a <string-array> array of strings. For example:
<string-array name="entry_values">
<item>pull</item>
<item>push</item>
</string-array>
You could then use it in XML:
<ListPreference
android:key="server_reception_mode"
android:title="#string/title_server_reception_mode_preference"
android:entries="#array/entries_server_reception_mode_preference"
android:entryValues="#array/entry_values" />
If all you're after is avoiding translation, you can use the convention used in the Android sources: they have resource files named values/donottranslate.xml, values/donottranslate-names.xml, etc.
Looking at the documentation for the entryValues attribute, no:
Must be a reference to another resource [...]
Of course, just because you have a string resource doesn't mean you have to localise it.
You'll need to create an array in your resources. You can't declare it directly like you were hoping to.
For those kind of arrays, I usually create a second XML file with all entries that do not need to be translated (like array-notranslate.xml). That way, if you hand your xml files to somebody for translation, they won't have to deal with it.