Variables in android strings.xml

Variables can be used in the strings defined in strings.xml using the c programming like printf  ‘%d’ syntax. This is because getString (int resId, Object… formatArgs) passes the string through Formatter.
First define a string in the strings.xml file (usually res/values/strings.xml).

[xml]
<string name="unread_messages">You have %d unread messages.</string>
[/xml]

This string has a variable %d that will be replaced in the next step of our android java code.

[java]
String message = getString(R.string.unread_messages, 25);
[/java]

In the example above, getString substitutes %d for the integer 25 in the string message.
Output will be: You have 25 unread messages.

Incoming search terms:

  • tushroy com
  • www tushory com
  • turshroy com
  • www tuchroy com
  • www tushroy com download
  • www tushry
  • www tushroy in

Tushar Roy

Software Engineer (Backend engineer), Android Developer,

Leave a Reply

Your email address will not be published. Required fields are marked *


*