Android
strings.xml
parameters
localization
duplicate

Are parameters in strings.xml possible?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, parameters in Android strings.xml are possible through format placeholders. This is the standard way to insert dynamic values such as names, counts, and dates while keeping text localizable. The key is choosing the correct placeholder type and formatting with resources APIs rather than manual string concatenation.

Use Format Placeholders in strings.xml

Define placeholders like %1$s, %2$d, or %1$.2f in string resources.

xml
1<resources>
2    <string name="welcome_user">Welcome, %1$s!</string>
3    <string name="items_count">You have %1$d items.</string>
4    <string name="price_value">Price: %1$.2f</string>
5</resources>

Why numbered placeholders matter:

  • translators can reorder placeholders for grammar,
  • multiple parameters remain unambiguous,
  • formatting remains consistent across locales.

Pass Parameters from Kotlin or Java

Kotlin usage:

kotlin
1val userName = "Ava"
2val message = getString(R.string.welcome_user, userName)
3
4val count = 5
5val countText = resources.getQuantityString(R.plurals.item_count, count, count)

Java usage:

java
String message = getString(R.string.welcome_user, "Ava");
String countText = getResources().getQuantityString(R.plurals.item_count, count, count);

Always use getString or resource APIs so Android applies locale-sensitive formatting correctly.

Use plurals for Count-Dependent Text

For quantities, prefer plurals instead of trying to handle singular or plural manually.

xml
1<plurals name="item_count">
2    <item quantity="one">You have %1$d item.</item>
3    <item quantity="other">You have %1$d items.</item>
4</plurals>

This gives translators proper control over language rules beyond simple English patterns.

Escaping and Formatting Rules

Some characters require escaping in XML, and format syntax must remain valid.

Example with literal percent sign:

xml
<string name="progress_text">Progress: %1$d%%</string>

If you need apostrophes, use escaped form or surrounding quotes based on lint recommendations.

Also enable formatting checks in Android lint. It catches placeholder mismatches between default and translated resources.

Placeholder Reordering for Localization

Different languages may change word order. Indexed placeholders support this safely.

xml
1<!-- English -->
2<string name="last_login">%1$s logged in at %2$s</string>
3
4<!-- Another locale may reorder -->
5<string name="last_login">At %2$s, %1$s logged in</string>

Without index markers, reordered parameters can break at runtime.

Avoid Concatenation and Build Messages in Resources

Instead of:

kotlin
val text = "Welcome, " + userName

Prefer resource-driven formatting:

kotlin
val text = getString(R.string.welcome_user, userName)

This keeps UI text fully translatable and consistent.

Format Numbers and Dates with Locale Awareness

When placeholders include numeric or date values, format those values using locale-aware APIs before passing them into getString.

Kotlin example:

kotlin
1import java.text.NumberFormat
2import java.time.LocalDate
3import java.time.format.DateTimeFormatter
4import java.util.Locale
5
6val locale = Locale.getDefault()
7val amountText = NumberFormat.getCurrencyInstance(locale).format(1234.56)
8val dateText = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd", locale))
9
10val message = getString(R.string.invoice_summary, amountText, dateText)

strings.xml:

xml
<string name=\"invoice_summary\">Amount %1$s, date %2$s</string>

This keeps localization consistent and avoids forcing translators to parse raw machine formats.

Translation Safety and Lint Validation

Android lint can detect placeholder mismatches between languages. Keep placeholders aligned across all localized strings.xml files and run lint in CI. Placeholder mismatch errors are easier to fix during pull requests than after runtime crashes.

Also keep placeholder indexes stable over time. Renumbering placeholders carelessly can break older translations during incremental localization updates. Consistent review checklists help prevent this regression.

Common Pitfalls

A common mistake is using unnumbered placeholders in strings that may be translated into languages with different token order. Use indexed placeholders such as %1$s to avoid reorder bugs.

Another issue is passing wrong data types for placeholders, such as string for %d. This can throw runtime formatting exceptions.

Developers also hardcode dynamic text through concatenation, which blocks localization and causes inconsistent phrasing across screens.

Summary

  • Parameters in strings.xml are supported through format placeholders.
  • Use indexed placeholders to support localization-safe reordering.
  • Use plurals resources for count-dependent text.
  • Format strings via getString and related resources APIs.
  • Avoid manual concatenation to keep text translatable and maintainable.

Course illustration
Course illustration

All Rights Reserved.