Dynamic String Using String.xml?

When I first saw %1$s and %2$d in the accepted answer, it made no sense. Here is a little more explanation.

String format replacement markers in xml are in the form of

%[parameter index]$[format type]

parameter index: If you had three parts of the string that you wanted to replace, then they would be called %1, %2, and %3. The order you place them in the resource string doesn’t matter, only the order that you supply the parameters in String.format.

format type: There are a lot of ways that you can format things (see the documentation). Here are some common ones:
$s string
$d decimal integer
$f floating point number

Escaping characters

Use %% to get the actual % character.

For more details read the following helpful tutorial.

Android SDK Quick Tip: Formatting Resource Strings

Example

Goal

Create the following formatted string where the gray parts can be replaced programmatically.

12 is an int and Hello is a String.

Solution

MyActivity.java

int myInt = 12;
String myString = “Hello”;
String myFormattedString = String.format(getString(R.string.my_xml_resource_string), myInt, myString);

string.xml

%1$d is an int and %2$s is a String.

Reference: https://stackoverflow.com/questions/3656371/dynamic-string-using-string-xml

Advertisements

Useful Link

FC Portables
Portable Apps For USB, Flash And Cloud Drives

FC Portables

Download Bull
Free Software Store

Download Bull

Get Into PC
Download Free Desired App

Get Into PC

Ganttpro
Ganttpro

Ganttpro

​​Wunderlist
​​Wunderlist

​​Wunderlist

Google Online Keyboard
Google Online Keyboard

Google Online Keyboard

Bengali Keyboard Online LEXILOGOS
Bengali Keyboard Online LEXILOGOS

Bengali Keyboard Online LEXILOGOS

Bengali Typing | English to Bengali Typing | Online Bengali Typing – Bangla
Bengali Typing | English to Bengali Typing | Online Bengali Typing – Bangla

Bengali Typing | English to Bengali Typing | Online Bengali Typing – Bangla

Vector Drawables
Vector Drawables

Vector Drawables

Shape Shifter
Shape Shifter

Shape Shifter

ASCII Table Generator Quickly format ASCII table
ASCII Table Generator Quickly format ASCII table

ASCII Table Generator Quickly format ASCII table

Format Text as Table
Format Text as Table

Format Text as Table