Handling Android dynamic text string in res/values - android

Let's say I have this string:
Your player has a good keeper skill and a decent people skill
Now the part not in bold of the string is always the same, what is known at runtime is the part in bold.
So how could I do something like:
Your player has a {var1} keeper skill and a {var2} people skill
and then fill those vars at runtime with right values?
I do not want to concatenate strings like:
"You player has a" + var1 + "keeper skill and a" + var2 + "people skill"

You need to see Android string resource guide. There is a way to provide static string which can be later formatted with variables.
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
You will define string like
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
And later in code you can replace
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);

in Strings.xml
You player has a %1$d keeper skill and a %2$d people skill
in java
getString(R.string.article_stats, var1, var2);

Yes, see the following from android devguide
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:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
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 arguements from your application like this:
Resources res = getResources();
String text = String.format(
res.getString(R.string.welcome_messages),
username, mailCount);

in strings xml you have define html syntax in CDATA tag like
<![CDATA[<b> %1$s bought </b>, last purchased from %2$s <b> %3$s </b>]]>
and in your java class
String detail = String.format(getString(R.string.detail),15,"New Delhi","23 mins ago");
detailView.setText(Html.fromHtml(detail ));

Related

android translate string with variables

good morning,
i have an android app with a german and english language xml file.
now i would like to set and text view like this:
"Hello User x, hello world y."
"Hallo Benutzer x, hallo welt y."
for x and y i would like set an variable.
how can i translate this text for both languages with dynamic variables ?
Your question seems unclear but from my understanding I am suggesting you the solution.
If you want to pass a dynamic argument to string you can do this by bellow example.
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
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
"Hello User %1$s, hello world %2$s."
"Hallo Benutzer %1$s, hallo welt %2$s."
In code then simply call getString(R.string.your_string, yourName, yourWorld) where yourName and yourWorld are String variables.
If you need to know where to store the strings see the answer of Devendra Singh.

Formatting Strings

People often link the following for explaining formatted strings, Can anyone show me a working example of how to use this?
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
well the name of the string is "welcome_message", for example you want to add that formated text into component, just edit layout.xml, find your component, and set the text on component to use that string
component < ....
android:text="#string/welcome_message
... />

Android: string format with Double value in Strings.xml

I need to create a String using the Formater to display some double values in application. I'm not clear on how to code it. Here is what I have:
<string name="diseaseMessage">You have %s message, Unread %s</string>
i get this error:
error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
error: Unexpected end tag string strings.xml /TSMS Pro/res/values line 70 Android AAPT Problem
<string name="diseaseMessage">You have %1$s message, Unread %2$s</string>
From the Android docs:
Formatting strings
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:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
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);

Android: strings resource, multiple strings with the same name

In strings.xml
Lets say I have the following strings:
<string name="CourseInfo">Course Information</string>
<string name="CourseInfo1">Course Information:</string>
<string name="CourseInfo2">Course Information:-</string>
As you can see the string is the same, the only difference is that the second one has a colon and the third one has a colon and a dash.
Is this the most optimal way to do this? It seems kinda repetitive to do it this way.
maybe you can use some thing like
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
see http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
so for your example
<string name="CourseInfo2">Course Information%1$s</string>
and then
Resources res = getResources();
String text = String.format(res.getString(R.string.CourseInfo2), ":");
String text2 = String.format(res.getString(R.string.CourseInfo2), ":-");

Are parameters in strings.xml possible? [duplicate]

This question already has answers here:
Is it possible to have placeholders in strings.xml for runtime values?
(14 answers)
Closed 5 years ago.
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.
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>
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();
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:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
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
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%.
Note that for this particular application there's a standard library function, android.text.format.DateUtils.getRelativeTimeSpanString().

Categories

Resources