Are parameters in strings.xml possible?

AndroidDynamicParametersStringOptional

Android Problem Overview


In my Android app I'am going to implement my strings with internationalization. I have a problem with the grammar and the way sentences build in different languages.

For example:

> "5 minutes ago" - English > > "vor 5 Minuten" - German

Can I do something like the following in strings.xml?

<string name="timeFormat">{0} minutes ago</string>

And then some magic like

getString(R.id.timeFormat, dynamicTimeValue)

This behaviour would solve the other problem with different word orders as well.

Android Solutions


Solution 1 - Android

Yes, just format your strings in the standard String.format() way.

See the method Context.getString(int, Object...) and the Android or Java Formatter documentation.

In your case, the string definition would be:

<string name="timeFormat">%1$d minutes ago</string>

Solution 2 - Android

If you need two variables in the XML, you can use:

%1$d text... %2$d or %1$s text... %2$s for string variables.

Example :

strings.xml

<string name="notyet">Website %1$s isn\'t yet available, I\'m working on it, please wait %2$s more days</string>

activity.java

String site = "site.tld";
String days = "11";

//Toast example
String notyet = getString(R.string.notyet, site, days);
Toast.makeText(getApplicationContext(), notyet, Toast.LENGTH_LONG).show();

Solution 3 - Android

> If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource: > Hello, %1$s! You have %2$d new messages. > > In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this: > Resources res = getResources(); String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

If you wish more look at: http://developer.android.com/intl/pt-br/guide/topics/resources/string-resource.html#FormattingAndStyling

Solution 4 - Android

There is many ways to use it and i recomend you to see this documentation about String Format.

http://developer.android.com/intl/pt-br/reference/java/util/Formatter.html

But, if you need only one variable, you'll need to use %[type] where [type] could be any Flag (see Flag types inside site above). (i.e. "My name is %s" or to set my name UPPERCASE, use this "My name is %S")

<string name="welcome_messages">Hello, %1$S! You have %2$d new message(s) and your quote is %3$.2f%%.</string>

Hello, ANDROID! You have 1 new message(s) and your quote is 80,50%.

Solution 5 - Android

Note that for this particular application there's a standard library function, android.text.format.DateUtils.getRelativeTimeSpanString().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondhesseView Question on Stackoverflow
Solution 1 - AndroidChristopher OrrView Answer on Stackoverflow
Solution 2 - AndroidPedro LobitoView Answer on Stackoverflow
Solution 3 - AndroidGabriel LucasView Answer on Stackoverflow
Solution 4 - AndroidGorioView Answer on Stackoverflow
Solution 5 - AndroidDivideView Answer on Stackoverflow