Wednesday, July 25, 2012

Use of variables in strings.xml in Android

e.g X company have Y employees
where X is a string variable and Y is an integer variable.

We can use this type of case in R.string also , as discussed below:

Write in strings.xml file
<string name="abc">%1$s company have %2$d employees.</string>
where $s stands for string
$d for integers

In java file
String mystring = getResources().getString(R.string.abc);
String finalstring = mystring.format(mystring, "Google",10000);

OUTPUT:
Google company have 10000 employees.

1 comment:

Unknown said...

Hello! In case you need to translate your Android strings in multiple languages, I suggest to analyze https://poeditor.com/. It is a software localization tool having a friendly and flexible interface, as well as plenty of useful features: API, translation memory, GitHub and BitBucket integration. There is also a localization plugin available to make the translation process easier via the POEditor API.