Android Access Value String Resource
- The resources folder for an Android app contains further directories and files, including images for user interface elements, data items and text strings. The resources folder typically holds a set of sub-folders inside it. The sub-folders categorize the resources for an application according to types, which include menu definitions, data values, layout declarations and images. As long as a developer maintains this standard structure within her application package files, the Java code is able to access the resources contained within it.
- Android applications can include multiple types, including text strings, digital images and animations. Many of these resource types are defined inside XML files, including text strings. Android applications can model text string data items in XML, as well as specifying user interface elements such as icons and labels. The location of a resource is normally tailored to its type. For string resources, an app will usually store an XML file inside the "values" folder in the "res" directory, with any file name as long as ".xml" is used as the extension.
- In an Android application, developers can use strings to model data items as well as user interface elements. For example, the titles and labels that appear on the buttons and screens within an application can all be defined as string resources. Rather than coding these strings directly within the Java processing code for an application, modeling them as resources allows the code to re-use their values, without unnecessary repetition. This way, if the developer needs to alter the value of an application string, she only needs to make the change in a single location.
- Developers often need to access the value of a string resource from within an application's Java code. The following sample code demonstrates the technique:
R.string.title_string
This allows the code to access a value stored in an XML file within the "res/values" directory, with the following markup code:
<resources>
<string name="title_string">My Application</string>
</resources>
The string element's name attribute provides a reference point for the Java code. Using this process, the developer can refer to the title text string inside the application logic. Other resource files in XML can also refer to the string as follows:
@string/title_string
This is a common practice, particularly in layout files in which an application user interface is being declared.
Resources Folder
Resources Types
Strings
Java Access
Source...