in the case my topic is a duplicate please link the corresponding topic, but I didnt find exactly what I'm looking for.
In my app I have a prefs.xml file which contains a list prefeference named "list" with the values "default theme" and "other theme". Those values can be selected by the user in a PreferenceActivity. In some other activity, I have to check WHICH value was selected and call one of two methods (setUpDefaultTheme or setUpOtherTheme) which I've created already. That check must be made just after the activity starts so the user sees one of those two themes, depending on the option the was selected in Preferences.
I know that I have to use SheredPreferences to do so, but I cant manage to get it working..
Try this
shared preferences in android
or this
https://stackoverflow.com/search?q=shared+preferences
Related
When I originally wrote and published my app, I was using a custom written activity to handle application settings. I used custom file name to store shared preferfences, like this:
getSharedPreferences("custom_settings_file",MODE_PRIVATE);
But now I'm refactoring my app and I would like to implement PreferenceActivity or PreferenceFragment and an xml file with PreferenceScreen section. Every tutorial or example that I've seen is using
getDefaultSharedPreferences(context);
to retrieve shared preferences, because PreferenceActivity assumes default filename to store preferences and there's no way to tell it to use a different one(at least I couldn't find one after an hour of searching and reading documentation).
So now I have a problem. If I just simply use the new default file, existing users of my app will lose their settings when they update the app, because the new application will not know anything about "custom_settings_file". What would be the best way to move the data from an old file to a new one on app update?
Here are the possible options that I could come up with:
Extend Application class and implement a piece of code in onCreate() so that every time my app is launched, it would check for existence of "custom_settings_file" and move it's contents to the new one. But running a block of code on every app launch seems like wasting too much processing resources for an operation that only needs to run once.
Just notify the user that their old settings are gone. But obviously this is not acceptable.
Is there a better solution, than option 1? Perhaps someone has already faced a similar problem?
What is preventing you from doing number 1 only once?
Just add a "migration" boolean to the new sharedpreferences.
If you also load the xml preference file then you can try this:
PreferenceManager.setDefaultValues(context, YOUR_PREFERENCE_NAME, MODE_PRIVATE, R.xml.preference_file, false);
If not (you want to add each preference item dynamically in your code) then you can do like this:
PreferenceManager pm = getPreferenceManager();
pm.setSharedPreferencesMode(MODE_PRIVATE);
pm.setSharedPreferencesName(YOUR_PREFERENCE_NAME);
In case you still want to use the defaultSharedPreference and process the migration then ... I'm writing this and I see Nicklas's answer, so I'm done here.
Could you add value in your new SharedPreferences that records whether you are a new install or an upgrade. If you don't have the setting in your sharedpreferences, check to see if you have an old preferences file in the way you were before. Then convert those preferences to your new method, and set your private setting indicating that it's been upgraded. Then just set the new value indicating the new state and you won't need to check your old preferences any more.
So, what I'm actually looking for is this solution: Get/pick an image from Android's built-in Gallery app programmatically but working in a preference and not in an activity.
I created a class that I put in my setting and I want to use that class to be able to find the URI of an image selected by the user on its phone. I couldn't use the code in that link because of "startActivityForResult". Which is not available in a preference.
Any idea how to bypass this ?
PROGRESS REPORT:
So, I tried some stuff in the mean time. Instead, in my settings, I added the following:
<Preference
android:key="test"
android:title="open image"
android:persistent="true">
<intent android:action="android.intent.action.GET_CONTENT"
android:value="image/*"/>
</Preference>
First, instead of "GET_CONTENT", it was "PICK", but with it, I can only go with Google Docs and the Sim Card Tool Kit, which is far from the gallery or any thing to browse the file system.
With "GET_CONTENT", it crashes.
I also added the "android.permission.WRITE_EXTERNAL_STORAGE" premission, even if I just want to read. But I didn't found any (let put this as a sub question, if there's a way to just ask to read and not write).
PROGRESS REPORT #2
I replaced android:value by android:mimeType and I goes directly to the gallery. Now, just need to know if I really got the URI and it's solved.
PROGRESS REPORT #3
At this point, here's the real problem. When I go through my SharedPreferences, the value stays empty, even after selecting an images. So, I guess there a little hack to do. So now, that's the question. Based on the example of my preference screen above, how can I retrieve the value of "test", assuming that when the intent is called, it put it somewhere ?
I finally found a solution... it's not as feng shui as I would like, but it works.
I simply code the preference screen by hand and then I was able to access the "startActivityForResult" function.
From this point, it took 3 to 5 minutes to solve it and finalize all the details.
Hope it helps some people.
You have an example there: http://www.anddev.org/code-snippets-for-android-f33/preference-screen-with-custom-views-t8472.html
But the one I used was on google, but I couldn't find the link.
So I have this big stupid problem with the preferences in my live-wallpaper.
First, PreferenceManager.getDefaultSharedPreferences, doesn't work. I'm calling it in my Main class, a subclass of WallpaperService in the function onCreate. For the parameter, I first tried "this" and then this.getBaseContext(), but it doesn't matter. So, when I print the values, nothing shows up!
Second, I saw in another answer here that to put some default values, use something like PreferenceManager.setDefaultValues(this.getBaseContext(), R.xml.setting, true);. So, this, doesn't get my defaultValues at all. They're all zeros and even one values from my setting.xml doesn't show up in the list. I explicitly put a android:defaultValue for each of them.
Note that once I put values for each of them in the preferences, this problem doesn't happen. But still, for someone who installs my app, it needs to work on the first time it's launched.
Preferences are a bear. I always start with a working example, and then modify it to suit my needs; it's impossible to remember the formalisms :-). The "obvious" place to start is the "second" Cube example in the SDK, which uses a preferences activity to choose between cube and dodecahedron shapes. For something more sophisticated, you might wanna look at Moonblink's "Substrate" package, which combines multiple wallpapers, and has complex settings. Project home is here: http://code.google.com/p/moonblink/wiki/Substrate , then click Source at top left, then Browse, Trunk, Substrate, src, etc. GF
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.
I'm creating my first android app (or attempting to anyway) and i have a question i can't seem to find the answer to.
I would like to allow users to enter a separate set of preferences for each item in a list view. I have the PreferenceScreen working, but it maintains the same preferences for every item (i.e. changing 1 item results in changes to all others as well).
To give a more detailed example:
Say i have a list of cars:
* Car 1
* Car 2
* Etc...
I would like to be able to click on "Car 1" and get a PreferencesScreen to specify "Make", "Model", "Color" for just that car, instead of having those entries stored globally for the whole app.
Is this possible?
Thanks for your help!
//N
Solved it!!
For any wondering, the solution is that you must call
getPreferenceManager().setSharedPreferenceName(value) with a value unique to the item you'd like to modify the preferences for (in my case, i used the ListView position) prior to calling addPreferencesFromResource.
getPreferenceManager() deprecated on api 11
Now with honeycom and ICS the method getPreferenceManager() is deprecated. Do you know how to do that with the new (PreferenceFragment + Headers)?
Thanks men! It works perfect in earlier versions!
UPDATE: Up to api level 11 is the same method but you have to call it on a Fragment, not on activity.