I am struggling to restore the default values I specified in the preferences.xml, Here is my code:
Preference reset = findPreference(res.getString(R.string.DEFAULT_PREFS));
reset.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference p) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Preferences.this);
sp.edit().clear().commit();
PreferenceManager.setDefaultValues(Preferences.this, R.layout.preferences, true);
return true;
}
});
This code is my understanding of the android's developers reference for the function setDefaultValues(context, resId, readAgain):
Parameters
context The context of the shared preferences.
resId The resource ID of the preference hierarchy XML file.
readAgain Whether to re-read the default values.
Note: this will NOT reset preferences back to their default values.
For that functionality, use getDefaultSharedPreferences(Context)
and clear it followed by a call to this method with
this parameter set to true.
Well, it does not work, the preferences values are the same after this code is executed.
Then I looked into the SharedPreferences variable sp, and it points to a system generated file in the path:
/data/data/<packagename>/shared_prefs/<packagename>_preferences.xml
which I can only assume is the same xml I provided when I created the activity.
addPreferencesFromResource(R.layout.preferences);
Also inspecting the sp variable, the hash table has all the preferences, but there is no field for default value.
EDIT:
Before I am asked to, here is an excerpt from the preferences.xml file
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:defaultValue="5000"
android:key="#string/MAX_MESSAGES"
android:numeric="integer"
android:summary="#string/MAX_MESSAGES_desc"
android:title="#string/MAX_MESSAGES_title" />
<EditTextPreference
android:defaultValue="10"
android:key="#string/VIEW_EDGE_ROWS"
android:numeric="integer"
android:summary="#string/VIEW_EDGE_ROWS_desc"
android:title="#string/VIEW_EDGE_ROWS_title" />
<ListPreference
android:defaultValue="0"
android:entries="#array/level_list"
android:entryValues="#array/level_values"
android:key="#string/INITIAL_ORG"
android:summary="#string/INITIAL_ORG_desc"
android:title="#string/INITIAL_ORG_title" />
<ListPreference
android:defaultValue="2"
android:entries="#array/view_list"
android:entryValues="#array/view_values"
android:key="#string/INITIAL_VIEW"
android:summary="#string/INITIAL_VIEW_desc"
android:title="#string/INITIAL_VIEW_title" />
<CheckBoxPreference
android:defaultValue="true"
android:key="#string/AUTOSCROLL"
android:summary="#string/AUTOSCROLL_desc"
android:title="#string/AUTOSCROLL_title" />
<CheckBoxPreference
android:defaultValue="true"
android:key="#string/SEND_THEN_EXIT"
android:summary="#string/SEND_THEN_EXIT_desc"
android:title="#string/SEND_THEN_EXIT_title" />
<Preference
android:key="#string/DEFAULT_PREFS"
android:summary="#string/DEFAULT_PREFS_desc"
android:title="#string/DEFAULT_PREFS_title" />
</PreferenceScreen>
This is my solution so far. After debugging into the Android source code I find out that setDefaultValues() is not reliable and does not work as intended (at least according to my expectations).
I restore the default values manually now. I have a map where I can fetch from the default values.
Here is an interesting note: Inspecting the Preference class shows it has a field called mDefaultValue which contains the default value for the preference. But this field can only be set by setDefaultValue() method, and there is no method to get it. It would have save me the need for a Map
This is the code I use now, tested and working:
Preference reset = findPreference(getResources().getString(R.string.DEFAULT_PREFS));
reset.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference p) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Preferences.this);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
// PreferenceManager.setDefaultValues(Preferences.this, R.layout.preferences, false);
for(int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
restoreDefault(editor, getPreferenceScreen().getPreference(i));
}
editor.commit();
return true;
}
}
private void restoreDefault(Editor editor, Preference p) {
if(p instanceof PreferenceCategory) {
PreferenceCategory pCat = (PreferenceCategory) p;
for(int i = 0; i < pCat.getPreferenceCount(); i++) {
restoreDefault(editor, pCat.getPreference(i));
}
}
if(p instanceof ListPreference) {
editor.putString(p.getKey(), resMap.get(p.getKey())._default);
}
if(p instanceof EditTextPreference) {
editor.putString(p.getKey(), resMap.get(p.getKey())._default);
}
if(p instanceof CheckBoxPreference) {
editor.putBoolean(p.getKey(), resMap.get(p.getKey())._default.equals("true"));
}
}
Another note: editor.commit() updates the preferences file, but does not update the preferences screen. You have to update each preference by using the listener (OnSharedPreferenceChangeListener()).
Related
I am using the following methods to save and read user settings:
private void saveUserSettings(){
SharedPreferences userSettings = getSharedPreferences("userSettings", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = userSettings.edit();
editor.putInt("timeOne",timeOne);
editor.apply();
}
private int getUserSettings(){
SharedPreferences userSettings = getSharedPreferences("userSettings", Context.MODE_PRIVATE);
timeOne = userSettings.getInt("timeOne",timeOne);
}
Then in onCreate the following:
SharedPreferences prefs = getSharedPreferences("userSettings", Context.MODE_PRIVATE);
This is fine and the data is saved when the app relaunches. However I want to have default data when the app is installed initially it seems to be that the values should be stored in an xml file.
I have created the following file under res/xml/preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="timeOne"
android:defaultValue="2"/>
</PreferenceScreen>
Then in onCreate:
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
I changed the "userSettings" to preferences to match up but this dosen't work and returns a value of zero. Is this method of reading the xml file ok or/and am I overlooking something?
I think you are overcomplicating yourself.
In this instruction the second parameter is the default to use if there is no shared preference with that name.
You just need to set that value to the default you need.
timeOne = userSettings.getInt("timeOne",<Put here the default value>);
EDIT I
Lets say the default value if it is the first time the app runs and there is no setting saved yet, is 2.
The method that reads the value should be like this.
private int getUserSettings(){
SharedPreferences userSettings = getSharedPreferences("userSettings", Context.MODE_PRIVATE);
timeOne = userSettings.getInt("timeOne",2);
}
Okay so I'm trying to make sense of how Shared Preference works with Preference.
Below is part of an app that works, but I do not understand how it works for the part of Shared Preference.
I have a UserPrefActivity.java containing the following code
public class UserPrefActivity extends PreferenceActivity {
//a preference value change listener that updates the preference summary
//to reflect its new value
private static Preference.OnPreferenceChangeListener
sBindPreferenceSummaryToValueListerner = new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
//gets string value of object
String value = newValue.toString();
Log.e("in sBind Listener", value);
//if the preference is a list preference, get the value at a given index
if (preference instanceof ListPreference) {
//for list preference look up the correct display value
//in the preference entries array
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(value);
//set the summary to reflect the new value
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
} else {
//set summary to only value
preference.setSummary(value);
}
return true;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//call the deprecated method to load the preference screen
addPreferencesFromResource(R.xml.user_pref);
//get default Shared preference from preference screen
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Preference saveNumPizza = findPreference("saveNumPizza");
//set the preference change listener to saveNumPizza
saveNumPizza.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListerner);
sBindPreferenceSummaryToValueListerner.onPreferenceChange(saveNumPizza,
PreferenceManager
.getDefaultSharedPreferences(this)
.getString(saveNumPizza.getKey(), ""));
}
}
In the MainActivity.java, I have the following as part of a bigger piece of code
//gets the default shared preference instance and assign to sharedPrefs for enabled save
// data
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences
(MainActivity.this);
//checks if save order status is true
if (sharedPrefs.getBoolean("saveOrder", false)) {
//do stuff..
}
I have the following UserPref.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:defaultValue="false"
android:key="saveOrder"
android:summary="for quick loading of favourite order"
android:title="Enable Saving Order"/>
<PreferenceCategory android:title="ORDER HISTORY">
<CheckBoxPreference
android:defaultValue="false"
android:key="keepHistory"
android:summary="of pizza orders"
android:title="Keep History"/>
<ListPreference
android:defaultValue="3"
android:dialogTitle="Choose how many to save"
android:entries="#array/number_of_pizza"
android:entryValues="#array/number_of_pizza_values"
android:summary="3 pizza orders"
android:key="saveNumPizza"
android:title="Number of Orders"/>
</PreferenceCategory>
</PreferenceScreen>
I know that in the UserPrefsActivity, I believe in the PreferenceManager.getDefaultSharedPreferences(this) call, the shared preferences is created and obtained if it does not exist, if it does its just obtained.
From what I've noticed with the app, is that on first launch when I get to the settings and launch the UserPrefActivity via intent, the shared preference of the UserPrefActivity is created, containing only the saveNumPizza key. However the minute I click on the other preferences (Checkbox preference) when I look at the shared preference file, I immediately see that the saveOrder key and keepHistory key are automatically saved.
I would just like to know how this automatic saving is exactly done since I am not using an editor to save it to the shared preference, nor am I calling apply or commit.
Thank you,
I am not able to identify the issue with my code and it seems no one face this kind of issue, so not able to track this in stack overflow.
Exception message:
Caused by: java.lang.ClassCastException: android.preference.CheckBoxPreference cannot be cast to android.preference.SwitchPreference
Code:
private Preference preference;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
addPreferencesFromResource(R.xml.settings);
loadAllViews();
}
private void loadAllViews()
{
if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH)
{
preference = (SwitchPreference) findPreference("preference"); //Exception occurs here.
}
else
{
preference = (CheckBoxPreference) findPreference("preference");
}
}
__________________ __________________ __________________
settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="preference"
android:title="yes_or_no" />
</PreferenceScreen>
Some one please help me to figure it out.
CheckBoxPreference does not extends SwitchPreference; hence it cannot be cast to it. Both of these classes are children of TwoStatePreference.
From your code, it seems that you are referencing one preference key for two different preference components:
if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH)
{
preference = (SwitchPreference) findPreference("preference"); //Exception occurs here.
}
else
{
preference = (CheckBoxPreference) findPreference("preference");
}
As you can see, this will work fine for the correct preference type, but it will throw a ClassCastException for the incorrect type. Make sure you are referencing the correct key for the correct TwoStatePreference.
<?xml version="1.0" encoding="utf-8"?>
<SwitchPreference
android:key="preference1"
android:title="yes_or_no" />
<CheckBoxPreference
android:key="preference2"
android:title="yes_or_no" />
In the code:
Preference preference;
if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH)
{
preference = (SwitchPreference) findPreference("preference1");
}
else
{
preference = (CheckBoxPreference) findPreference("preference2");
}
Need to have two separate layout.
One for check preference at layout directory.
And another for SwitchPreference at layout-v14 directory.
I am trying to get preferences from XML but without using a PreferenceActivity.
I just want to load from the file when my Main activity is created and toast a value from preferences. But the problem is that the toast is empty (null?). I have a class that load the preferences.
Here is the onCreate method of the Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Preferences prefs = new Preferences();
}
Preferences class (only the constructor...)
public Preferences(Context context) {
PreferenceManager.setDefaultValues(context, "MyPrefs", 0, R.xml.preferences, false);
SharedPreferences sharedPreferences = context.getSharedPreferences("MyPrefs", 0);
Editor editor = sharedPreferences.edit();
String myValue = sharedPreferences.getString("myKey", null); // I don't know if null is OK
Toast.makeText(context.getApplicationContext(), myValue, Toast.LENGTH_SHORT).show();
}
And the XML file
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference android:key="myKey" android:defaultValue="hello" />
</PreferenceScreen>
The first time I launch the app, I would like the default value to be set. So here I want the app to toast "hello" while I haven't set the value with editor.putString(key, value).
Do you know what could be wrong?
Thanks
You are programming in java. By suns convention I think you are obliged to use config.propeties file.
I will give a quick and full tutorial to get you going in this matter. I really recommend you using this method cause most of programmers do like that.
I will give you a quick tutorial how to make this file. Where to put it. And how to get data from it.
Begin.
Put a file config.properties into assets folder:
SAMPLE of config.properties
domain=#domain.com.pl
errorTextColor=\#FF0000
serverPort=1234
Method how to access and retrieve data from config.properties
public static String getConfigurationPropertiesValue(String value,
Context context) {
try {
Resources resources = context.getResources();
AssetManager assetManager = resources.getAssets();
try {
InputStream inputStream = assetManager.open("config.properties");
Properties properties = new Properties();
properties.load(inputStream);
return properties.getProperty(value);
} catch (IOException e) {
Log.e("getConfigurationPropertiesValue",
"Failed to open config property file");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Literature:
http://en.wikipedia.org/wiki/.properties
http://www.mkyong.com/java/java-properties-file-examples/
EDIT:
You can also use sharedPreferences to have more control over data like adding data/deleting data/update data. SharedPreferences are more like a SQLite database of android with a nice api to use so you don't really need to know the location of database or SQL.
In order to use it you need to create your data. You only need to do this once. Or more if user decides that he has an urge to clean you app data from settings -> applications.
Creating data:
public static void create(Context cw) {
SharedPreferences sharedPreferences = cw.getSharedPreferences(
ANDROID_MESSENGER, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isRegistered", false);
editor.putString("phoneNumber", null);
editor.putString("callingCode", null);
String uuid = UUID.randomUUID().toString();
editor.putString("token", uuid);
editor.putBoolean("internetOnly", false);
editor.putBoolean("logToDev", true);
editor.putBoolean("dataTransfer", true);
Log.i("create", "Generating Token: " + uuid);
editor.commit();
}
Accesing existing data:
public static String getToken(Context cw) {
SharedPreferences sharedPreferences = cw.getSharedPreferences(
ANDROID_MESSENGER, Activity.MODE_PRIVATE);
return sharedPreferences.getString("token", null);
}
Updating data:
public static void setPhoneNumber(Context cw, String phoneNumber) {
SharedPreferences sharedPreferences = cw.getSharedPreferences(
ANDROID_MESSENGER, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("phoneNumber", phoneNumber);
editor.commit();
}
For an mechanism that will check if data exist or if user deleted it you can use something simple like an additional variable that should be true if all your data is configured. Or there should be an checked method from appshared preferences for that.
Cheers!
I tried for a long time and found a solution for this :-)
Just a "Preference" is not being recognized by android. Though i don't know why.
If you change it as a Any of the tag like "EditTextPreference" or "CheckboxPreference" its working fine.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference android:key="CLIENT_HOMESCREEN_TITLE"
android:defaultValue="Home Screen"/>
<EditTextPreference android:key="CLIENT_ADMIN_BUTTON"
android:defaultValue="Admin"/>
<EditTextPreference android:key="CLIENT_PLAYER_BUTTON"
android:defaultValue="Player"/>
<EditTextPreference android:key="CLIENT_SAVE_BUTTON"
android:defaultValue="Save"/>
<EditTextPreference android:key="CLIENT_CANCEL_BUTTON"
android:defaultValue="Cancel"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_IP"
android:defaultValue="IP:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_PORT"
android:defaultValue="Port:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_TITLE"
android:defaultValue="Server Setup"/>
</PreferenceScreen>
I'm writing an android app with a preferencesActivity in which selections made in my instance of preferencesActivity affect the values of other preferences items displayed. While I'm able to change the values of the underlying SharedPreferences items pogrammatically, those changed values aren't reflected in the displayed list items until I exit my preferencesActivity and reload it. Below is a stripped down version of my settings class and xml file which illustrate the problem. If a user sets Guitar as the value for the preference with the key instrumentList, I'd like the preference with key tuningChoice to revert to Standard.
//necessary import declarations go here
public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
app_preferences.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {
Log.d("onSharedPreferencesChanged", "sharedPreferences changed. key: " + key);
Editor preferencesMod = sharedPreferences.edit();
String instrumentChoice = sharedPreferences.getString("instrumentList", "Guitar");
if(key.equals("instrumentList")) {
Log.d("Settings", "key is instrumentList. chooseTuning before if: " + sharedPreferences.getString("chooseTuning", "no luck"));
if(instrumentChoice.equals("Guitar")) {
preferencesMod.putString("chooseTuning", "Standard");
preferencesMod.commit();
Log.d("Settings", "chooseTuning after if: " + sharedPreferences.getString("chooseTuning", "ciao"));
}
}
}
}
xml file preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Settings">
<ListPreference android:title="Choose an Instrument" android:key="instrumentList" android:entryValues="#array/instruments" android:entries="#array/instruments"/>
<ListPreference android:title="Choose Tuning" android:key="chooseTuning" android:entryValues="#array/tuningChoices" android:entries="#array/tuningChoices" android:persistent="true"/>
</PreferenceScreen>
I can call addPreferencesFromResource again in my onSharedPreferenceChanged method and that loads a duplicate of all the preferences items, displayed below the old items, with the correct values. If I could figure out some way to cancel out the initial addPreferencesFromResource called during onCreate, I guess I would be set.
Any help would be appreciated, Thanks
I do something along these lines...hopefully it helps:
ListPreference list = (ListPreference) getPreferenceManager().findPreference("myList");
list.setValue(sharedPrefs.getString("myList", "default"));
list.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
sharedPrefs.put("myList", newValue.toString());
return true;
}
});
You need to prevent addPReferencesFromResource from running twice? Is this loading your default values? If so, add an additional SharedPreference called DEFAULTS_LOADED and read its value in on create like: (WARNING PSUEDO CODE):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean loadDefaults = app_preferences.getBoolean(DEFAULTS_LOADED, true);
if(loadDefaults)
{
addPreferencesFromResource(R.xml.preferences);
Editor editor = app_preferences.edit();
editor.putBoolean(DEFAULTS_LOADED, true);
editor.commit();
}
app_preferences.registerOnSharedPreferenceChangeListener(this);
}
This will prevent you defaults from being written to the shared preferences every time your activity starts. I assume this is at least a portion of your issue.
If anyone comes to this problem, this is the solution:
ListView list = preferenceActivity.getListView();
list.performItemClick(list, 1, list.getItemIdAtPosition(1));
Maybe I am too late for answering this. But, I hope this might help beginner like me.
PackageInfo packageInfo = null;
try {
packageInfo = preference.getContext().getPackageManager().getPackageInfo(preference.getContext().getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
preference.setSummary(packageInfo.versionName);