Shared Preferences and Preference Fragment - android

Here are my questions:
How can I get the value of a switch preference?
Are things inside a preference fragment already a shared preference?
If so, how can I access shared preferences from a preference fragment?

1. To get the value of a switch:
CompoundButton cb = (CompoundButton)view.findViewById(R.id.myswitch);
if(cb.isChecked())
cb.setChecked(false);
else
cb.setChecked(true);
Reference: Toggle Buttons
2. Yes, the elements in a PreferenceFragment are automatically stored.
3. To get the SharedPreference object of a PreferenceFragment, use:
getDefaultSharedPreferences(Context context)

This is an amazing example on how to make a PreferenceFragment:
http://android-er.blogspot.hu/2012/07/example-of-using-preferencefragment.html
Basically the way it works is that you define the preferences with their types and keys. The preference fragment stores its data in the SharedPreferences, which you obtain via Android getDefaultSharedPreferences , and edit its values and obtain them from an Editor.
In a Preference Fragment, you automatically have the data of the prefs linked between the Fragment and the SharedPrefs.
Also look at this example code: http://www.mysamplecode.com/2011/11/android-shared-preferences-example_12.html

Related

How to retrieve data prior to running an activity

I have 2 fragments.
Fragment 1
Loads sharedpreference to display string
Fragment 2
Saves sharedprefence for string
Is it possible to retrieve that string in my first Fragment without running the second Fragment?
Yes, this is possible. You just need to make sure you are reading with the same key you used to write with:
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
// Reading from SharedPreferences
String value = prefs.getString("myKey", "defaultValue");
Log.d(LOG_TAG, value);
Note that we've assigned a defaultValue as the return value here. If there is no value with the key "myKey" in your shared prefs, it will instead return "defaultValue". This is a nice safeguard, think of it like a null pointer check - you will always get a value from getString(), even if it's just the default.
You don't need to be in the same activity for this to work, you just need to make sure that 1) your preferences name is the same and 2) the key used to store the value is the same in both spots.
First, don't get confuse between Activity and Fragment.
And yes, you can.

How to use findPreference outside of PreferenceActivity?

I'm trying to gain access to a (custom) list preference and change its selected value (i.e. if index 3 is selected, change the selection to index 1). However, findPreference() can only be used inside a PreferenceActivity. I need to access the preference and change its selection inside a regular Activity. Is there a way this can be done? I don't see anything in SharedPreferences that I can use to change the selection, only a list preferences value.
This is how you do it,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference(this);
prefs.getString(PREF_KEY, "default");

Accessing SharedPreferences

I have a problem in the LiveWallpaper app (my first) I am developing.
Consider 2 classes: LiveWallpaperService and LiveWallpaperSettings.
LiveWallpaperSettings extends PreferencyActivity. Example data representing
the preferences selected by the user, for example a boolean displaySprite (true=> display the sprite on the screen, false do not display) are saved/persisted via SharedPreferences in LiveWallpaperSettings.
Upon starting the application (Settings -> Display -> LiveWallpaper -> MyLiveWallpaper), the saved preferences need to be known so that the sprite can be displayed or not.
However, LiveWallpaperSettings is not instantiated until the Settings button is clicked, so SharedPreferences is not available, and thus saved settings are unavailable until then.
I tried this in LiveWallpaperService.onCreateScene(), but it has no data in it:
SharedPreferences startupPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
What can I do?
There is a default value if the entry/sharedpref file does not exist:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
boolean display = settings.getBoolean("display", true);
"public abstract boolean getBoolean (String key, boolean defValue)"
Added in API level 1
Retrieve a boolean value from the preferences.
Parameters
key The name of the preference to retrieve.
defValue Value to return if this preference does not exist.
Hope I didnt misunderstood your question :)
In your preferences xml set the default value and in your MainActivity onCreate() add the following code setDefaultValues(this, R.xml.yourxmlname, false);

Acces Preferences From another class

I have an application which has a Prefernces Class and I want to know how could i make so that when the application is started the settings to be applied even before entering the preferences ( settings ) class. I have a getPrefs() void method which is called when i press "Save" Button in preference activity.
So, could you help me and tell what should I do the "default" preferences to be applied when entering the application ? (I need getprefs method from another class )
I would be grate if you could give me some advices or tips.Thank you !
To get an instance of the SharedPreferences from anywhere in your application use:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPrefences(context);
To set a value in the preferences, you need to call the editor for those preferences, then set the value for a key and finally commit the result. It can all be done in a single line:
prefs.edit().putString("myKey","myValue").commit();
This would store the string value myValue on a key named myKey and it will be accessible (after you commit) to any class if it has the application's context when it calls getDefaultSharedPreferences.
To retrieve the stored value you specify the key and a fallback value in case there is no preference set with that key:
prefs.getString("myKey","oops no value found");

How to set a DefaultSharedPreferences value?

I have a preferences activity with a checkbox "Enable Service".
I read the value like this :
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(con);
ServiceEnabled_Pref = prefs.getBoolean("EnableService", true);
ok, but how can I set this Preference ?
this is not a CustomShared Preference, It is a DefaultShared Preferences and it seems that there is not a method putBoolean for DefaultSharedPreferences.
I need this, cause I have a Widget with a button that needs to set this value to true/false
programatically:
prefs.editor().putBoolean("EnableService", true).commit();
however your best bet for defining the preferenceActivity and its defaults is via an xml file as decribed here with the android:defaultValue attribute
Use the shared preferences editor:
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

Categories

Resources