Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value of a string resource in constants file?

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.

like image 884
Usama Sarwar Avatar asked Aug 10 '11 09:08

Usama Sarwar


People also ask

In which folder can you find the string resource file strings XML?

The XML resource files containing localized strings are placed in subfolders of the project's res folder.

How do we define a string resource?

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.

What are constants in 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.


2 Answers

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.

like image 86
devconsole Avatar answered Sep 17 '22 19:09

devconsole


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.

  • http://developer.android.com/guide/topics/resources/string-resource.html
like image 42
Justin Shield Avatar answered Sep 17 '22 19:09

Justin Shield