Using SharedPreferences for application settings. Android - android

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?

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.

Related

Difference between writing text/string in java file and in string.xml file in values folder of android

I have created a class called Params.java in my project where i am writing all string parameters and accessing them as static wherever they are required.
Params.java
public static final String USERNAME = "username";
public static final String USER_TOTAL_FOLLOWERS = "user_total_followers";
public static final String USER_EARNINGS = "user_earnings";
public static final String USER_PROFILE_PICTURE_URL = "profilepic";
I also have strings.xml
<string name="btn_txt_cancel">Cancel</string>
<string name="btn_txt_okay">Okay</string>
<string name="btn_txt_not_now">Not Now</string>
<string name="btn_txt_update">Update Now!</string>
To access from Params, i simple need to write Params.variable_name and form strings.xml, i need to write getResources().getString(R.string.variable_name)
I want to know
What is the best practice ?
What difference it makes in performance when accessed from these to files ?
When you add a string in strings.xml, that means in current/future you can support multiple languages with the same key used in all language strings.xml file.
For normal constants like a key for using in bundle, better to create a class which holds all constants.
Finally the strings which are displayed in UI, the best practice is to add those in strings.xml and for code related usage constants use a class like Constants.class which holds all your constants.
Ok, So the best practice in android to store string is, Those strings are visible to user put them on string.xml file, for example hint of EditText "enter your name" and those string which are not going to visible to the user like your "Google API key" put them on Constant.java file.
This is the best practice while coding in Android.
The biggest advantage of using string.xml instead of a custom file, are translations.
Android Studio provides everything you need for translating your application simply adding a new resource localized for the locale you want.
This allows you to automatically change the lenguage without coding anything except the translated text.
(You can also buy translations from the Google Developer Console)
In your code you would have to add a switch and manually manage all cases.
More information can be found here
About performances
It's pretty much the same. they both are static resources with different access ways. If there is any performance impact it would be really hard to notice it, we are talking about milliseconds
you can use getString(R.string.stringName); from xml
for me i use string.xml for String Which are Visible to User. it has an Advantage when i m creating multiLanguage App.
for String that are not visible to users I prefer Params.java Like URL etc.
To support multiple languages and to separate concerns between view and logic, use strings.xml
If it is a string that used only in code and not visible to user, use a constants class.
This way you have a more cleaner code.

What should be the name for values folder for developer english in android

What i want is the name of value folder which consists of String.xml , In that XML i have written key-key pairing i.e "NAME"
"EMPLOYEE_ID". If i use any language key word say en,fi, that will not a good practice. So what should be the name of value folder for above string.xml.
Thank you.
Bydefault for english language it will pickup from values folder and for supporting languages you can refer this link
You should save this xml in the values folder only. Android is not bothered about what the keys are being mapped to; it just makes sure it returns the right value respecting the overrides if any for a particular device's Locale settings.
So, whether the key maps to a value or to some value's key itself, has no bearing on the values folder it should go in; but it should preferably be stored in a different xml file, say, string_keys.xml or something.
values/strings.xml
<string name="employee_name">Name</string>
values/strings_keys.xml
<string name="employee_name_key">employee_name</string>

Where to store string values?In strings.xml or in constants class?

In android, we can store string values either in strings.xml file or in some constants class as static final variable.Is there some reason for selecting one over another in some circumstances?
In a nutshell:
value for use in code: use always constants class.Advantage: codes remain integrated and your package can be utilized in other projects/contexts. You can not do that with string.xml as it is not transported with your package.
value for display in UI: use string.xml. Advantage: you can use localization to display translated texts.
Some situation may arise when both option appears viable. You will have to then decide where are its related values are stored.
As a general rule, use the strings.xml because android uses that XML to enable translating your app into different languages, which it can't do with strings that are hardcoded.
The official android guide on localization say the following;
Move all strings into strings.xml
As you build your apps, remember not to hard code any string. Instead
declare all of your strings as resources in a default strings.xml file
which makes it easy to update and localize. Strings in strings.xml
file can be extracted, translated and integrated back into your app
(with appropriate qualifiers) without any changes to compiled code.
If you generate images with text, put those strings in strings.xml as
well, and regenerate the images after translation.
Strings that are not going to be displayed to the user in any way needn't be stored in the XML, because they will never need translating, and you probably don't want the android system tampering with them in ways you might not know about during runtime.
If the string value is used to display in UI store in Strings.xml Otherwise keep it in code. There can be JSONTags, Key for different api/Thirdparty libraries.These kind of things should be kept in code itself.
strings.xml it is used for localization and needs a context to retrieve the content of a String. If you need a java constant to be accessed in different classes, you a public static final String member. If the string is a message for the user you should use strings.xml
If strings represent text readable by user, and which could potentially be translated to other languages (names of buttons, labels, notification/error messages, etc.) then they should be in strings.xml (actually, it can be any file name you like, not just "strings").
If string is some constant which is used in the app internally (bundle/intent keys, fragments tags, etc.) they should be declared in class
It depends, if it is a text string that will be translated or displayed to the user then for 118n sake, you will want to put in into strings.xml.
However, if the string is something like a server url or api code then you'll want to store those in code as a public static final String

Is it possible to change the value of a string added in res/strings.xml on Android at run time/ or by code?

I know we can use resource strings to store values, but is it possible to change those values at run time?
For example, I added two new resource elements username and password, and I want to change these values at run time. Or is there an alternate way to store values?
String resources are absolutely static defined; you can't change their values. Use SharedPreference which is to store your data, and you can change, update or do whatever to suit your needs.
Here is a sample for using SharedPreference: How to use SharedPreferences in Android to store, fetch and edit values
strings.xml are for constant strings only. Their values can change automatically when the phone language changes provided that you have created several strings.xml files such as strings-us.xml, strings-fr.xml.
You can store username & password values in Preferences. They will be stored permanently with your application.

Android set String value in String.xml

can anybody help me ?? I want to add String value through coding in string.xml
I am doing this.
String name = getResources().getString(R.string.name);
if(name.lenght() < 1 ){
// getResources().setString(R.string.name);??????????????????????
}
My string.xml is
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name"></string>
</resources>
does anybody know how i can add value of name in string.xml though coding.
Thank you!!
The resources are pretty much set in stone, so you can't modify them at runtime. If you need to store some new strings, use SharedPreferences or SQLite.
I don't think you want to do that. If you are trying to store a value in a persistent way, take a look at SharedPrefences. Google has a good introduction to it here.
It is not possible to modify the resources of an APK during runtime.
You can't edit those resources directly. You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file.
You cant edit a resource or add a resource once the code is compiled. I dont know exactly what setResource does, but once your program is compiled, android builds the gen files which designate a certain amount of space for those variables, changing the variable once written would cause overflow or outofbounds errors with memory. If you want persistent values try using the SharedPrefs, SQL or even your own XML stored within the directory of the app, which you could set to only be readable by your app.

Categories

Resources