For string constants the values folder can already be used to store global values.
Can the same thing be done for numeric values?
Or am I obliged to declare all the constants in a singleton (or Application implementation)?
If you need to store integer values you can still use Integer Resource Style. Unfortunately float values are not supported as resource types. Take a look at official documentation for further informations.
Eventually you can always use a simple class full of static fields if that fits your needs.
Related
I want to use the string resources to save some key values. So, I have something like this:
<string name="key_name">key_name</string>
The resource name and the resource value are the same. There is no need for me to make a difference between them, as it is just to cleanly store one value.
To reduce redundant information,
is there a way to just tell android-studio that the name equals the value? Something like this?
<string name="key_name"/>
I think you can't do it. It looks like you create a variable, you must name it and when you need you can set its value.
You can't create a variable with same values with name at the same time.
I think you have the wrong resource type. A string resource is used for mapping a name to a text. From what you're describing here you only need the name (and then can extrapolate the text from it).
Using strings with matching name and value shouldn't be necessary:
If you're going to be referencing them from Java or Kotlin code, you still need to know the name (i.e. R.string.key_name), so you might as well just use the string value there.
If it's for using inside XML code (where you want to pass the value of a string, e.g. text="#string/key_name") most of the time you can just use the raw string there (i.e. text="key_name").
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.
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
I would like to have sequence of set R.drawable been called in integer array so that it can be accessed later with choice.But I'll like to load to the R.drawable dynamically which corresponds to different names.It names needs to be had as per the external value input not hardcoded.I tried this in making many but like use list,set,array conversion etc.Kindly guide me with a snippet or example on this regard.Thank you.
I suppose, that best way to achieve this funcionality is by using ArrayAdapter of Integer.
You will simply add drawble resources by id.
If you need to get resource id, but you will get string input from user, you can use this code:
Android, getting resource ID from string?
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.