how to read value from string.xml in android?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Android externalizes user-facing text into res/values/strings.xml so that strings are easy to translate, reuse, and maintain. The build system generates an R.string class with integer IDs for each entry, and the framework provides several methods to resolve those IDs into actual string values. This article covers basic string definitions, formatted strings, plurals, string arrays, and how to access resources from different Android contexts.
Defining Strings in strings.xml
The resource file lives at res/values/strings.xml. Each <string> element has a name attribute that becomes the generated constant in R.string.
Names must be valid Java identifiers: lowercase letters, digits, and underscores. Avoid spaces or hyphens.
Reading Strings in an Activity or Fragment
Any class that extends Activity or Fragment has access to getString() through its inherited Context.
In Java the call is identical:
The returned value is a plain String with all XML escaping resolved.
Using Strings in XML Layouts
Reference string resources directly in layout attributes with the @string/ prefix:
This approach keeps layouts free of hardcoded text and enables the layout preview in Android Studio to display localized strings.
Formatted Strings with Placeholders
Use standard java.util.Formatter placeholders to inject dynamic values:
Resolve the placeholders with getString():
The %1$s syntax means "first argument, formatted as a string." The %2$d means "second argument, formatted as a decimal integer." Always use positional notation (%1$, %2$) rather than bare %s so that translators can reorder arguments for different languages.
Quantity Strings (Plurals)
Android provides <plurals> for strings that change based on a quantity:
Read the plural with resources.getQuantityString():
The first count selects the plural form; the second count fills the %d placeholder. The supported quantity values are zero, one, two, few, many, and other. Which forms are used depends on the language: English only uses one and other, while Arabic uses all six.
String Arrays
For lists of related strings, use <string-array>:
Read the array in code:
String arrays are commonly used to populate Spinner and ListView adapters without writing additional code.
Accessing Strings from Non-Activity Classes
Classes that do not extend Activity or Fragment need a Context reference to access resources.
In a ViewModel, use AndroidViewModel which receives the application context:
Avoid passing Activity references into long-lived objects like singletons or ViewModels, because the activity can be destroyed while the reference is still held, causing a memory leak. Always prefer applicationContext for long-lived components.
Accessing Strings from a Service or BroadcastReceiver
Both Service and BroadcastReceiver are subclasses of Context, so getString() is available directly:
Common Pitfalls
- Hardcoding strings instead of using resources. Lint warns about this with the
HardcodedTextinspection. Hardcoded strings cannot be translated and make refactoring harder. - Using bare
%sinstead of positional%1$s. Translators in some languages need to reorder arguments; bare format specifiers break when arguments are swapped. - Editing the wrong locale file. Translated strings live in
res/values-<locale>/strings.xml(for examplevalues-esfor Spanish). Changes in the wrong folder will not appear for that locale. - Holding an Activity context in a long-lived object. This prevents garbage collection of the destroyed activity. Use
applicationContextfor singletons, repositories, and ViewModels. - Expecting resource changes without rebuilding. After renaming or deleting a string resource, the
Rclass must be regenerated by rebuilding the project. Stale references cause compile-time errors.
Summary
- Define all user-facing text in
res/values/strings.xmland reference it throughR.stringconstants. - Use
getString(R.string.name)in Activity, Fragment, Service, or any class with aContextreference. - Use positional format specifiers (
%1$s,%2$d) for dynamic values so that translators can reorder arguments. - Use
<plurals>for quantity-dependent strings and<string-array>for ordered lists of strings. - Always pass
applicationContextrather than an Activity reference to long-lived components to avoid memory leaks.

