Android: Default preference value not being set from XML - android

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.

Related

How to set default theme for LatinIME in AOSP

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>

SharedPreferences default values

I have a settings.xml file that contains Preferences for my app. All the values by default are set to "true" and its used by PreferenceActivity in my app.
In my main activity I read the values through
SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(appContext);
Boolean key = sp.getBoolean("M", false);
it gets me whatever I need at any run except the first time. Only when I open my app for the first time and doesn't open my settings menu I get "false". I mean I have to open menu and only after that the app run correctly.
Any suggestions?
Because yor are getting the default value for first time.
that is false -->
Boolean key = sp.getBoolean("M", false);
use this
Boolean key = sp.getBoolean("M", true);
Use sp.getBoolean("M", true); instead. Default values in preferences file is what it will be when preferences activity is first started. SharedPreferences know nothing about your settings.xml.
Alternatively you can have a separate defaults.xml file (or any other name) containing all the default values for all preferences. Then you use these values in both settings.xml (#bool/default_M_value) and your application (sp.getBoolean("M", getResources().getBoolean(R.bool.default_M_value))).
I personally would stick to first approach though.

Added preferences dynamically that will last over sessions

I have preference defined in xml, and I do this addPreferencesFromResource(R.xml.preferences1); to create the pref activity. But in code (dynamically) at runtime somethimes(depends on busyness logic) I add more items like this
CheckBoxPreference c=new CheckBoxPreference(this);
c.setKey("asdasd");
c.setTitle("asd");
getPreferenceScreen().addPreference(c);
This works great and everything is fine(the state is saved corectlly and it stays persistent) until I restart the phone. When I restart the phone this newly added item is lost. And only items from the xml file are shown in the prefereceActivity.
My question is what should I do to have this items even when the user restart the phone.

Android, subclass Preference class

I am tryig to write an Android Honeycomb application and I am having trouble subclassing Preference: http://developer.android.com/reference/android/preference/Preference.html
I want to make a similar layout with title and summary but also a progress bar.
I have created the layout and added the custom preference class but I can't seem to get hold of the instance of it to set the values of the items in it.
It seems that the preference key doesn't work for the custom class.
Here is my preference definition compared to the standard preference class:
<Preference
android:key="int_free_storage"
android:title="Free Space"
android:summary="free storage value here"/>
<com.hamid.storageether.SpacePreference
android:key="int_space_test"
android:title="Test"
android:summary="This is my custom preference"/>
My my preference subclass then sets my XML layout as it's layout resource in its constructor
setLayoutResource(R.layout.space_pref_layout);
it also overrides the setTitle and setSummary methods....
In my main PreferenceActivity I try to get hold of my Preference by it's key but no luck it seems, since the preference never gets updated:
// These Two work
Preference intTotal = (Preference)findPreference("int_total_storage");
Preference intFree = (Preference)findPreference("int_free_storage");
intTotal.setSummary("Standard Preference Summary 1");
intFree.setSummary("Standard Preference Summary 2");
// My subclass doesn't - It just displays the default text defined in the layout xml.
SpacePreference intTest = (SpacePreference)findPreference("int_test_space");
intTest.setTitle("Testtttyyy");
intTest.setSummary("Test Summary");
Could someone please point me towards where I may be going wrong?
Is this code copied straight from the program or retyped? If copied, then your key is "int_space_test" in XML and "int_test_space" in code. It should be throwing a null pointer exception on the next line where you use intTest if that's the case.

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