I'd like to know what is the best way to use shared preferences for application settings, namely to change text size and text colour. The tutorials I am finding are all confusing and most of them are using deprecated methods. What is the best way to proceed for API 17?
Asked
Active
Viewed 709 times
1
-
http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Ali Imran Dec 08 '12 at 14:29
1 Answers
1
Create a preferences screen using this so your users have a place to change values. In your code, check the value of the keys you used in this preference screen and do whatever you need to do.
By the way, the example in the Android doc I linked shows hard-coded key string literals. The best practice way to do this is to create string keys in strings.xml resource file and reference the string key in your preference screen xml file and in your java code.
For example, in strings.xml:
<string name="wifiEnabled">wifi enabled</string>
In you preference screen xml file:
<CheckBoxPreference
android:key="@string/wifiEnabled"
android:title="WiFi" />
In your java code:
String wifiEnabledStringKey = getString(R.string.wifiEnabled);
//this will give you just 'wifi enabled'; you can then use this to retrieve the value of this key from SharedPreferences.

praneetloke
- 1,953
- 1
- 14
- 15
-
Hello, I have been using this system to, but I start to dislike it because you have to duplicate the keys when you translate the app to other languages. Even these keys, which aren't visible to the use need to be duplicated, but not translated. If you don't do this, LINT will complain. Do you also have this issue? – bvanvelsen - ACA Group Aug 01 '13 at 08:35
-
I don't understand what you mean by duplicate. If by that you mean creating the same key in another language, then yes, that should be done. That is how the OS knows the key is available in another language as well besides just the default language. There are folders for each language. As long as you have a strings.xml file in the respective language and create the same key in it, the resource loader will know which key to use based on the language setting on the device. – praneetloke Aug 08 '13 at 01:43
-
yes, but preference keys make no sense in other languages, because they are the same in every language, and not even visible to the end user. Thats why I don't like this system to use them for preferences, but I do use them for every string which is visible to the user(preference title or summary) – bvanvelsen - ACA Group Aug 08 '13 at 05:07