Formatting Strings - android

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
... />

Related

how can I solve this error string xliff?

I have this string:
<string name="order_summary_name">Name: <xliff:g id="name" example="Thomas">%s</xliff:g></string>
using it in this format:
priceMessage=getString(R.string.order_summary_name,name);
and I keep getting an error that says: Format string 'order_summary_name' is not a valid string.
I cant find the mistake,what should I do ???
Store your styled text resource as an HTML-escaped string:
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
In this formatted string, a element is added. Notice that the opening bracket is HTML-escaped, using the < notation.
Then format the string as usual, but also call fromHtml(String) to convert the HTML text into styled text:
String text = getString(R.string.welcome_messages, username, mailCount);
String styledText = Html.fromHtml(text);
(or)
String styledText = Html.fromHtml(R.string.order_summary_name,name);
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
The id attribute is just used to identify what the substitution parameter represents (in your case, it represents the quantity). It's as you said, a note, and not actually used programmatically.
That said, in Android Studio, if you have Code Folding enabled for strings, it will substitute in the ID when it shows the collapsed string. You'd see something like this:
Try this.
mTextView.setText(getString(R.string.order_summary_name, "Thoma"));

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.

Android, Is it possible to format textview like System.out.format("%02d", n);?

I have a table with different columns. instead of creating text view for each item i want to have one textview for each row. i want to add each item one by one to row string. because some items may have 1 digit and some others may have two digits, i want to format it.
in Java we do something like:
intn = 7;
System.out.format("%0d", n); // --> "07"
is it possible do same way with textview? if yes how to do that. Thanks
http://developer.android.com/guide/topics/resources/string-resource.html
Formatting strings
<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);
It is possible, refer to this
String text = String.format("%0d", n);
textView.setText(text);
Create a format string in resource file, :
<string name="message">Test: %d, %s</string>
then call (you can use unlimited number of arguments):
String str = context.getResources().getString(R.string.message, 100, "hello" );

Handling Android dynamic text string in res/values

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 ));

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