i am working on an application where i have to get a value of string resource to a constant. how can i do that? any ideas please.
The String is placed in
Application/res/values/strings.xml
and the name of the resource is let say app_version. now i want to get this app version to a constant string in another file. Any help is appreciated.
The XML resource files containing localized strings are placed in subfolders of the project's res folder.
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.
A string constant is an arbitrary sequence of characters that are enclosed in single quotation marks (' '). For example, 'This is a string'. You can embed single quotation marks in strings by typing two adjacent single quotation marks.
Try context.getResources().getString(R.string.app_version)
where 'context' is your Activity or Application instance.
If your question is really about getting the version name of your application there is a better way:
String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
This way you retrieve the info directly from the AndroidManifest and you don't have to maintain it twice.
In Java: R.string.string_name
In XML:@string/string_name
R.string.app_version
or @string/app_version
taken from strings.xml is your constant and will always represent the value you entered into that XML file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With