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"))
Related
I need some guidance with setting default theme for LatinIME on AOSP. I am not quite sure where this value is stored.
First I tried setting the theme in ThemeSettingsFragment.java located in LatinIME. So now everytime a theme was set or changed it would always pick mine. Later on I found out this class is only called when we open Keyboard themes in Settings (Language & Input -> Android Keyboard (AOSP) -> Appearance & layouts -> Theme). Resulting in theme being changed only IF we opened these view. My goal is to have my theme set when I build AOSP.
Next I suspected the value could be stored in some global configuration and that led me to class InputMethodManagerService.java where I found constant Settings.Secure.DEFAULT_INPUT_METHOD. But that didn't lead me anywhere worth while.
Anyone ever worked on something similar or knows the solution to my problem?
You can try making changes here : https://github.com/LineageOS/android_packages_inputmethods_LatinIME/blob/cm-14.1/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java#L56-L58
I hope it helps.
Sanyam Jain is right to the point, adding few more details to it.
packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java to be modified to change the default keyboard layout or you can add your custom layout there and also make sure the options added/changed correctly in packages/inputmethods/LatinIME/java/res/values/keyboard-themes.xml
In my case, I want material dark theme as default and first option in the keyboard layout settings. below are the changes I made,
KeyboardTheme.java
public static final int DEFAULT_THEME_ID = THEME_ID_LXX_DARK;
private static KeyboardTheme[] AVAILABLE_KEYBOARD_THEMES;
/* package private for testing */
static final KeyboardTheme[] KEYBOARD_THEMES = {
new KeyboardTheme(THEME_ID_LXX_DARK, "LXXDark", R.style.KeyboardTheme_LXX_Dark,
// This has never been selected as default theme.
Build.VERSION_CODES.LOLLIPOP),
new KeyboardTheme(THEME_ID_ICS, "ICS", R.style.KeyboardTheme_ICS,
// This has never been selected because we support ICS or later.
VERSION_CODES.BASE),
new KeyboardTheme(THEME_ID_KLP, "KLP", R.style.KeyboardTheme_KLP,
// Default theme for ICS, JB, and KLP.
VERSION_CODES.ICE_CREAM_SANDWICH),
new KeyboardTheme(THEME_ID_LXX_LIGHT, "LXXLight", R.style.KeyboardTheme_LXX_Light,
// Default theme for LXX.
VERSION_CODES.BASE),
};
In keyboard-themes.xml
<string-array name="keyboard_theme_names" translatable="false">
<item>#string/keyboard_theme_material_dark</item>
<item>#string/keyboard_theme_material_light</item>
<item>#string/keyboard_theme_holo_white</item>
<item>#string/keyboard_theme_holo_blue</item>
</string-array>
<!-- An element must be a keyboard theme id of
{#link com.android.inputmethod.keyboard.KeyboardTheme#THEME_ID_ICS} etc. -->
<integer-array name="keyboard_theme_ids" translatable="false">
<item>4</item>
<item>3</item>
<item>2</item>
<item>0</item>
</integer-array>
Hello fellow programmers, I have a little problem with Preferences activity.
http://developer.android.com/reference/android/preference/PreferenceActivity.html
I've got just one preference category and a listPreference:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="#string/basic_settings" >
<ListPreference
android:defaultValue="70"
android:entries="#array/listArray"
android:entryValues="#array/listValues"
android:key="updates_interval"
android:persistent="true"
android:summary="#string/SOME_SUMMARY"
android:title="#string/SOME_TITLE" />
</PreferenceCategory>
I need to have the selected value (the default one or the user defined one) written in the summary of the listPreference, for example:
We will have at least 70 characters.
How can I do this from the code?
Any help is appreciated
Try like this..
Preference customPref = (Preference) findPreference("updates_interval");<-- your preferences key
customPref.setSummary("desired string");
here is a short example:
Preference etp = (Preference) findPreference("the_pref_key");
etp.setSummary("New summary");
This requires that you display your preferences either from a PreferenceActivity or from a PreferenceFragment, since findPreference() is a method of these classes. You most likely do that already.
To change the summary every time the user changes the actual preference, use a OnPreferenceChangeListener and check if the relevant key changed in the callback. After it has changed, just edit the summary like above.
You can create a subclass of ListPreference in which you set an OnPreferenceChangedListener from which you will have access to the new value, and set the text on your ListPreference. I think the setSummary() function on the ListPreference will update the text under the name of the preference. If that doesn't work you can also override getView() to implement your own custom view for the Preference on which you can set the text directly.
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.
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.
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