How-to init a ListPreference to one of its values - android

I'm trying to set a defaultValue to a ListPreference item.
Here is an sample of my preference.xml file:
<ListPreference android:key="notification_delay"
android:title="#string/settings_push_delay"
android:entries="#array/settings_push_delay_human_value"
android:entryValues="#array/settings_push_delay_phone_value"
android:defaultValue="????">
</ListPreference>
The two arrays:
<string-array name="settings_push_delay_human_value">
<item>every 5 minutes</item>
<item>every 10 minutes</item>
<item>every 15 minutes</item>
</string-array>
<string-array
name="settings_push_delay_phone_value">
<item>300</item>
<item>600</item>
<item>900</item>
</string-array>
When i go into the preference activity, no item of the ListPreference is selected. I've tried to set an int value like 1 in the "android:defaultValue" fied to select "10 minutes" but it does not work.
<ListPreference android:key="notification_delay"
android:title="#string/settings_push_delay"
android:entries="#array/settings_push_delay_human_value"
android:entryValues="#array/settings_push_delay_phone_value"
android:defaultValue="1">
</ListPreference>
Any Idea?

You need to specify the value. So to get the first entry selected by default specify defaultValue="300" in your example.

Happened to be in same situation. Specifying a consistent default value. But graphically was not selected. I cleared the application data. And then it worked as expected.
So a clear may be useful at dev time when adding new XxxPreference items.

In addition to Sven's answer, you have to call the setDefaultValues() method in the starting activity. This will set once all default values.
public class MainActivity extends Activity {
protected void onCreate(final Bundle savedInstanceState) {
// Set all default values once for this application
// This must be done in the 'Main' first activity
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
...
}
}

If it is a valid value from the list, then re-install the app. It will work.

Related

How to set multiple default values in a MultiSelectListPreference?

I have preference.xml like this
<MultiSelectListPreference
android:key="store_select"
android:title="#string/setting_store_title"
android:summary="#string/setting_store_summary"
android:dialogTitle="#string/setting_store_dialog_title"
android:entries="#array/store_names"
android:entryValues="#array/stores"
android:defaultValue="#array/stores"
/>
with my two arrays:
<string-array name="stores">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
<string-array name="store_names">
<item>foodbasics</item>
<item>nofrills</item>
<item>metro</item>
<item>loblaws</item>
<item>sobeys</item>
</string-array>
I want the default behaviour to be all of the options selected, but currently nothing is selected by default. Am I doing something wrong?
To make all MultiSelectListPreference items selected (on) by default, then include the attribute defaultsValue for the Preference, e.g.
android:defaultValue="#array/stores"
If it's not working, then make sure that you clear the application data as this will only take effect the first time the application is run.
I think you forgot calling PreferenceManager.setDefaultValues(this, R.xml.preference, false);
in the onCreate() method of your mainActivity.
This method will read your preference.xml file and set the default values defined there. Setting the readAgain argument to false means this will only set the default values if this method has never been called in the past so you don't need to worry about overriding the user's settings each time your Activity is created.
I know I am late but may be my answer helps someone else in future...
set
android:defaultValue="#array/empty_array"
where empty_array is an empty array.
If you are adding MultiSelectListPreference programmatically then you can simply call multiSelectListPreference.setDefaultValue():
e.g.
val preference = MultiSelectListPreference(context)
preference.setDefaultValue(setOf("US, "CN"))

PreferenceActivity error: doesn't show selected option after close Activity

The value is saved well in SharedPreference when i push it , but it doesn't show when i open another time the PreferenceActivity. It runs if i don't put the android:entryValues , but i can't use it cause there is some difference using distinct languages in order to see what's the value of the prefference.
¿Any idea of what can i do?
Thanks for reading.
code:
the PreferencesMenu activity:
public class PreferencesMenu extends PreferenceActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setDefaultKeyMode(MODE_PRIVATE);
addPreferencesFromResource(R.layout.preferences);
getPreferenceManager().setSharedPreferencesName("Gat_Preferences");
}
}
some of strings.xml:
<string-array name="menu_preference_general_order_array">
<item>Default</item>
<item>Alphabetical</item>
</string-array>
<string-array name="menu_preference_general_order_values">
<item>default</item>
<item>alphabetical</item>
</string-array>
preferences.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
...
<PreferenceCategory android:title="#string/menu_preference_general">
<ListPreference
android:key="list_order"
android:persistent="true"
android:title="#string/menu_preference_general_order_title"
android:summary="#string/menu_preference_general_order_description"
android:entries="#array/menu_preference_general_order_array"
android:entryValues="#array/menu_preference_general_order_values"/>
</PreferenceCategory>
</PreferenceScreen>
mod :
I use android 2.1 , and i can't use the new fragments preference.
You need to tell the preference API what file name you want to use before loading everything up.
Instead of this:
addPreferencesFromResource(R.layout.preferences);
getPreferenceManager().setSharedPreferencesName("Gat_Preferences");
Do this:
getPreferenceManager().setSharedPreferencesName("Gat_Preferences");
addPreferencesFromResource(R.layout.preferences);
On a sidenote, don't use R.layout.preferences. You should use R.xml.preferences, putting the file under /res/xml and not under /res/layout. It does work your way, but it's not guaranteed to work in all API versions, since it's not the default way of working with preferences XML files.
Your preferences.xml should be in res/xml.
Also, you should assign the default value
<ListPreference
android:key="list_order"
android:persistent="true"
android:title="#string/menu_preference_general_order_title"
android:summary="#string/menu_preference_general_order_description"
android:entries="#array/menu_preference_general_order_array"
android:entryValues="#array/menu_preference_general_order_values"
android:defaultValue="default"
/>

Android: preferences not being stored automatically

I'm trying to use preference screen. I'm following all steps from online tutorial (once I couldn't get it working, I found other tutorials, and steps seem to be fine). I get to preferences screen, edit values, return to calling activity (via hardware return button). In DDMS perspective FileExplorer shows package_name_preferences.xml file with preferences that should be stored. It contains:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="false">kg</string>
</map>
while I expect (data line only shown).
<string name="weight">kg</string>
Also, if I go change only 1 preference, the same value changes, not a new row is created. I'm just tempted to write my own preference classes that would store data in files or DB, but I know that preferences should work, it just doesn't save properly my stuff.
Edit
Tutorials used:
Main Tutorial
- Was using this as a base, simplified, as I needed only 3 listPreferences so far.
Another One - Used this one back when first installed android, so referred to this one for its section on preferences
Code: (Screen loads, so I'm not showing Manifest)
public class MyPrefs extends PreferenceActivity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.my_prefs);
}
}
my_prefs.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Value Settings">
<ListPreference android:title="Distance"
android:summary="Metric (Kilometer) vs Imperial (Imperial)"
android:defaultValue="km"
android:key="#+id/distanceMesurement"
android:entries="#array/distance"
android:entryValues="#array/distance_values"/>
<ListPreference android:title="Weight"
android:summary="Metric (Kilogram) vs Imperial (Pound)"
android:defaultValue="kg"
android:key="#+id/weightMesurement"
android:entries="#array/weight"
android:entryValues="#array/weight_values"/>
</PreferenceCategory>
</PreferenceScreen>
calling MyPrefs from MainScreen
Intent i = new Intent(MainScreen.this, MyPrefs.class);
startActivity(i);
arrays.xml
<resources>
<string-array name="weight">
<item name="kg">Kilogram (kg)</item>
<item name="lb">Pound (lb)</item>
</string-array>
<string-array name="weight_values">
<item name="kg">kg</item>
<item name="lb">lb</item>
</string-array>
<string-array name="distance">
<item name="km">Kilometer (km)</item>
<item name="mi">Mile (mi)</item>
</string-array>
<string-array name="distance_values">
<item name="km">km</item>
<item name="mi">mi</item>
</string-array>
</resources>
Your key syntax is invalid. Use:
android:key="weight"

Android: Default preference value not being set from XML

I have a simple Preferences Activity that I populate via XML, defining the values as array resources.
In the MAIN Activity of the application, I get a handle to this via:
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
In the XML for the only preference I have, which is a display setting, I have the following XML that defines the ListPreference:
<ListPreference
android:title="#string/pref_title_sort"
android:summary="#string/pref_summary_sort"
android:key="#string/pref_key_sort"
android:defaultValue="modified"
android:entries="#array/sort_order"
android:entryValues="#array/sort_order_values" />
You can see I am trying to set the default value to 'modified', which is a value found in #array/sort_order_values:
<string-array name="sort_order_values">
<item>modified</item>
<item>created</item>
<item>name</item>
</string-array>
However, when the Preferences Activity is launched, none of the items are selected by default.
I've tried adding the following line to my Activity, but it did not change anything (where pref_main is the XML file that defines the preferences):
PreferenceManager.setDefaultValues(this, R.xml.pref_main, false);
Any help appreciated!
Paul
Maybe You have just set a wrong value (without corresponding item in the value array) at the first time you run application. Now Android remembers your first choice. Try to manually uninstall app (Menu >> Settings >> Applications >> Manage Applications >> >> Uninstall ). This should help.

Android: SharedPreference: Defaults not set at startup

I have Listpreferences in my app. They don't appear to be setting to their defaults right after installation - they appear to be null. I'm trying to figure out why my default preferences are not being set right after installation. In my main code I have:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
InUnits = sp.getString("List1", "defValue");
InAngs = sp.getString("List2", "defValue");
OutUnits = sp.getString("List3", "defValue");
OutAngs = sp.getString("List4", "defValue");
Right after the above code executes, each variable contains "defValue" instead of the actual values I have assigned in my ListPreference below.
My preference xml file is called, "settings.xml". Here's what one of the ListPreferences there looks like:
<ListPreference
android:key="List1"
android:title="Input: Alph"
android:summary="Choose Alph or Ralph"
android:entries="#array/inputAlph"
android:entryValues="#array/input_Alph_codes"
android:dialogTitle="Input Alph"
android:defaultValue="ININ"/>
Here's what some of my strings.xml file looks like:
<string-array name="inputUnits">
<item>Alph</item>
<item>Ralph</item>
</string-array>
<string-array name="input_Alph_codes">
<item>ININ</item>
<item>INMM</item>
</string-array>
When I go to menu, and then settings, I can see my defaults checked (radiobuttoned). Then when I go back from the settings menu to my main screen - all is well - for life! ...then each var above is assigned the proper default value.
This only happens when I first install my app on the phone. After I go to the settings screen once and then right out of it, the app is fine and accepts any setting changes.
By the way, as you can see, "List1" is the android:key within a file called settings.xml in my res/xml folder.
They don't appear to be setting to
their defaults right after
installation - they appear to be null.
That's what's supposed to happen.
I'm trying to figure out why my
default preferences are not being set
right after installation.
They're not supposed to be. The preference XML you have listed there is only used for populating a PreferenceActivity, nothing more. Until the user opens the PreferenceActivity, the preferences will be null, and the defaults you supply to the SharedPreferences getters will be returned.
UPDATE
You can use setDefaultValues() on PreferenceManager to assign the defaults from your preference XML to a SharedPreferences. However, be careful of the timing -- this will do disk I/O, and therefore ideally is performed on a background thread.
Set the default values to SharedPreferences from your preference XML.
PreferenceManager.setDefaultValues(Context context, int resourceId, boolean readAgain)
PreferenceManager.setDefaultValues
You can specify a default value like this
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.getString("thePrefKey", "theDefaultValue");
The android:defaultValue="..." in the "layout" settings.xml is only a visual help for user

Categories

Resources