Skip to content
Search

Android:Resource:String

lastmod 2014-06-12created 2014-01-155 edits

Android: Multiple substitutions specified in non-positional format

When working with Android ADT 2.3, you may sometimes get the Multiple substitutions specified in non-positional format error.

<string name="mystring">%s %s</string> 

The error occurs when you put in multiple substitutable items like this, and the fix is

<string name="mystring">%1$s %2$s</string>

This form is used for cases where the order changes in a particular Locale.

<string name="mystring">%2$s %1$s</string>

Here, 1$ and 2$ are the Argument order.

If you want to disable format checking, add the formatted="false" attribute as shown below.

<string name="mystring" formatted="false">%s %s</string> 

Plurals

To handle singular and plural strings, use Plurals.

Example

The XML example is as follows.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="numberOfSongsAvailable">
        <item quantity="one">One song found.</item>
        <item quantity="other">%d songs found.</item>
    </plurals>
</resources>

The Java code is as follows.

int count = getNumberOfsongsAvailable();
Resources res = getResources();
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);