How can I keep multiple sets of Preferences using the AndroidX Perference library?
For example, the user can choose different profiles which have different values over the same set of settings:
Global preferences:
Selected profile: foo
Other settings: etc
Profile 1:
Name: foo
Setting: Bar
Profile 2:
Name: bar
Setting: Hello
etc
I see two options here:
Use Context.getSharedPreferences(String name, int mode) with different names for different profiles. PreferenceManager has a setDefaultValues method that also takes a name in that case. The downside of this approach is that a settings screen will still use the default shared prefs, and not the ones for whatever profile is selected.
Make your own shared prefs wrapper that extends PreferenceDataStore and implements all the methods. Then call PreferenceManager.setPreferenceDataStore whenever the profile is changed. A settings screen will show the correct values with this approach. Internally, your wrapper can either use a database to store the values, or use the default shared prefs, appending a different prefix for different profiles.
I haven't tried either but I believe it could work.
Related
The Android documentation describes how to create a "preferences" UI using either a PreferenceActivity or a PreferenceFragment. The preferences themselves are defined in a XML file (e.g. preferences.xml) which contain, among other things, the "key" (android:key) to use to store each preference in the app's SharedPreferences.
This is good for apps which have a single set of preferences. However let's assume that there is an app that can have multiple "items" (for example: multiple account in a video chat app), and needs to show a preferences screen for each account.
How can this be done? Is it possible to specify the keys at runtime (so that e.g a different prefix can be used for each account)? Is there a better way to approach this?
Yes, its possible!
you can assign keys on Runtime like, but as you said in comments that you need to change the Keys of the pre-defined Preferences in Xml rather than creating new Preferences explicitly, here's a method -
Preference pref = findPreference("my_pref");
String myPrefix = "prefix_";
pref.setKey(myPrefix + pref.getKey());
In my application I am trying to add support for different profiles. Each of these profiles can have their own independent Preferences, configured from a deep tree of custom and stock preference objects.
I thought that I could add this easily by using different Preference Files per profile, as in getSharedPreferences("george", MODE_PRIVATE); would store them for profile "george".
The issue I am having is the Preference objects seem to be storing their values in the default Preference file. Is there any way from my Activity or Fragment to change which Preference file is used by the Preferences on my Preference Screen? I know this doesn't exist, but I'm looking for something like .setPreferenceFile(String filename) for a Preference or PreferenceGroup.
Any help or suggestions to minimize the work to support independent profile preferences will be appreciated..
i've been searching a for a while but couldn't find anything similar to what i'd need. I'd like to know how i can list all languages available in an android application? and say, pick one of them to run as default.
Just create a preferences Activity in your application and display all the supporting languages within it (may be in a list view with a check mark or something), when the user selects a specific language, just save the selected language preference in your Application Shared Preference.
Within every activity of your Application just run a conditional flow and use the Application's Drawables and String values with respect to the language selected in the Application Preference.
EDIT
Here is a good example of Localization
http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/
Since you don't want to check the selected language in the phone's settings, but only in the Application's context, then you should create a Preferences Activity specific to your Application. Here is a good Example of saving creating and using Preferences :
http://androidpartaker.wordpress.com/2010/07/11/android-preferences/
After doing that, just run a conditional flow in your Application's Activities, for example:
Class myActivity extends Activity
{
String KEY_SELECTED_LANGUAGE = "selected_language";
onCreate(...)
{
if(getApplicationContext().getSharedPrefrences().getString(KEY_SELECTED_LANGUAGE,"").equalsIgnoreCase("en"))
{
//change all the drawable/String values displayed in your activity to the version of the selected language.
}
}
I just curious. There are 3 method:
1. getPreferenceManager().setSharedPreferencesName(String PrefName);
2. PreferenceManager.getDefaultSharedPreferences(Context context)
3. Context.getSharedPreferences (String name, int mode)
As I know, the third method is only used when the first method is used, right?
But with 3 method we also use addPreferencesFromResource(int resID);
so, what is the difference? When can we use one of these method?
Thanks!
Let's go one step at a time:
setSharedPreferencesName() is method that allows to set the name of the preference group for later use. This is helpful for example when using the helper class of
PreferencesActivity before loading a preferences from XML resource file by calling addPreferencesFromResource(). It is therefore not as common as the other 2 methods you mentioned above.
getDefaultSharedPreferences() uses a default name, usually stored as /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml.
It is commonly used. Note that this default is set per application.
The alternative method - getSharedPreferences() requires to indicate a specific preference (file) name and an operation mode.
As appears also in another answer about shared preferences,
getDefaultSharedPreferences() in fact uses Context.getSharedPreferences, so the result is the same, but without the flexbility to split to multiple preference files, that is offered by getSharedPreferences(). Sharing the preferences between apps using
a MODE_WORLD_READABLE operation indicator is also something possible using getSharedPreferences(), but is rarely used.
IMHO, getDefaultSharedPreferences() can be safely used without going into the confusion of multiple preference file names that are prone to typos and confusion.
If someone knows of a good reason to use getSharedPreferences() and not getDefaultSharedPreferences(), please let me know by commenting here.
getDefaultSharedPreferences() uses a default preference-file name like "com.example.something_preferences". This default is set per application, so all activities in the same app context can access it easily as in the following example:
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
if (spref.contains("email")) {
String sEmailAddr = spref.getString("email", "");
}
The preferences are usually stored at /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml
getSharedPreference is the best way because using getDefaultSharedPreferences has some flaws
Actualy getDefaultSharedPreferences doesn't work correct on some
devices when build with targer api 13
Starting app from shortcut and from menu gives me different
DefaultSharedPreferences. After removing DefaultSharedPreferences
from my code - it works perfect. I can't just say: people dont make
shrotcuts, so I had to change code
This link may also help
I am looking for a way to create dynamic preferences where I don't need to hard code the preference key and I could have a variable number of preferences.
Basically, my application will let the user create a multiple number of profiles and each of these profiles will save custom values for a fixed number of preferences.
So this way, the user does not have to change the preferences every time he wants this app to run differently, he can just switch the profile.
One way I think will work is by subclassing all the standard Preference classes and calling their setKey method with my custom preference key, containing the profile name, but this is ugly.
So is there a cleaner and more standards compliant way to do this?
Regards,
Dhruwat
You can save different preferences in a different file for each user using the getSharedPreferences method:
getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
That way, you can do something like this:
SharedPreferences settings = getSharedPreferences("prefs_user_"+user_id, 0);
// from now on you can use use the normal way to set or get the preferences
I'm assuming you are using an id in order to identify them users.