android
strings.xml
resource-referencing
xml
android-development

Reference one string from another string in strings.xml?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Reusing string resources in Android is useful when the same phrase appears in several places and should stay consistent across localization updates. The main point is to distinguish between resource references and formatted strings, because they solve different problems. In practice, formatted placeholders are usually safer than trying to build large strings by nesting resource references directly. That keeps both maintenance and translation quality under control.

The Difference Between Referencing and Formatting

In Android resources, @string/name is primarily a resource reference used by layouts and attributes. Inside string text itself, the usual reusable pattern is string formatting with placeholders.

Example base strings:

xml
1<resources>
2    <string name="app_name">MyApp</string>
3    <string name="welcome_template">Welcome to %1$s</string>
4</resources>

Then format in code:

kotlin
val message = getString(R.string.welcome_template, getString(R.string.app_name))
textView.text = message

This is clearer and more localization-friendly than trying to hardwire one resource into another resource body.

Direct Resource Reference in XML Attributes

Where @string/... shines is in XML attributes:

xml
1<TextView
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:text="@string/app_name" />

That is a direct resource lookup, not a string-to-string composition feature.

Why Formatting Usually Wins

Using placeholders has several advantages:

  1. translators can reorder phrases naturally
  2. shared pieces stay centralized
  3. runtime composition is explicit

For example:

xml
<string name="screen_title">%1$s Settings</string>
<string name="brand_name">Acme</string>
kotlin
val title = getString(R.string.screen_title, getString(R.string.brand_name))

In some languages, the brand name or noun order may need to move. Placeholders support that.

When Nested String Reuse Gets Messy

Trying to compose long sentences from many little resource fragments usually hurts localization quality. Translators need the whole sentence context, not scattered pieces that only make sense in English word order.

Good reuse:

  • app name
  • short labels
  • product or feature names

Risky reuse:

  • long legal sentences assembled from fragments
  • grammar-sensitive phrases
  • text with plural or gender variation

For those cases, create one complete translatable string instead.

HTML and Formatting Together

If you also need styling, keep the placeholder approach and format first, then parse if needed.

xml
<string name="welcome_html">Welcome to &lt;b&gt;%1$s&lt;/b&gt;</string>
kotlin
val raw = getString(R.string.welcome_html, getString(R.string.app_name))

This keeps reuse and styling under control without duplicating brand names everywhere.

Plurals Need Separate Handling

If the reusable text includes counts, do not force normal string placeholders to do pluralization work. Use Android plural resources instead, then compose carefully in code where needed. Quantity rules differ across languages, so reuse should never undermine correct localization grammar.

Testing Localized Output

Whenever one string depends on another, test in at least one non-English locale. Reuse that feels tidy in English can become awkward in languages with different word order or inflection rules.

This is less about Android syntax and more about writing resource structures that survive translation.

Common Pitfalls

  • Assuming @string/... inside string text behaves like a general-purpose string macro system.
  • Building long UI sentences from small fragments and making localization awkward.
  • Forgetting that placeholders are usually the cleaner reuse mechanism.
  • Reusing strings with grammar that changes across languages.
  • Testing only English and missing broken phrasing in other locales.

Summary

  • Use @string/... directly in XML attributes to reference resources.
  • Use string placeholders and getString formatting when one string depends on another.
  • Prefer full translatable sentences over fragment assembly for localization-heavy text.
  • Reuse short stable pieces such as app names and labels carefully.
  • Validate reused strings in real localized output, not only in English.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.