Multiple values String format Android - android

I would to display text like this sample : "0/2 documents"
I'm trying to do this with :
<plurals name="documents_get">
<item quantity="one">%1d/%2d doucment</item>
<item quantity="other">%1d/%2d documents</item>
</plurals>
resources.getQuantityString(
R.plurals.documents_get,
docCount,
documents.filter {
it.retrieved_at != null
}.count(),
docCount)
My problem is the result is : 0/ 2 documents instead "0/2 documents". Space after the '/' is the problem.
Do you know a solution for this ?
Thanks in Advance

The problem is the format you are using (%2d). So change your plurals to this:
<plurals name="documents_get">
<item quantity="one">%d/%d doucment</item>
<item quantity="other">%d/%d documents</item>
</plurals>
Example with using String.format
String s1 = String.format("%d/%d document", 0, 1); // s1: "0/1 document"
String s2 = String.format("%d/%2d document", 0, 1); // s2: "0/ 1 document"
Edit
As #Kingfisher Phuoc noted, it should be %1$d/%2$d instead of %d. The $ sign allows you to specify the index of the string (the position where the string should be printed, called "explicit argument indices" or "positional arguments". You can read more here).
For example:
String s1 = String.format("%1$d/%2$d", 1, 2); // s1: "1/2"
String s2 = String.format("%2$d/%1$d", 1, 2); // s2: "2/1"

Remove (%2d) and replace it with (%d) or you can use string format
String resource = String.format("%d/%d document", 0, 1); // s1: "0/1 document"

You can define your string inside string.xml like this
<resources>
<string name="string">0"/"2 documents</string>
</resources>
and fetch it via getResources.getString method.
Note : Special Characters are enclosed inside double quotes.

Related

Can't get rupee symbol to show correctly from strings.xml

So this works fine:
strFoo = "\u20B9" + strBar
But this doesn't
strFoo = R.string.rupee_symbol.toString() + strBar //.toString() is required
//R.string.rupee_symbol.toString() evaluates to some random number 2131755148... which I believe is a character array...
strings.xml
<string name="rupee_symbol">\u20B9 </string>
I can't figure out why it would behave like that, it looks like the same thing...!
You should not concatenate strings with string resources instead, you can use place holder:
<string name="rupee_symbol">\u20B9%s</string>
And use:
strFoo = resources.getString(R.string.rupee_symbol, strBar)
use getString(R.string.rupee_symbol) instead R.string.rupee_symbol.toString()
For example-
String strBar = String.valueOf(100);
String strFoo = getString(R.string.rupee_symbol)+strBar;
textView.setText( strFoo);

how to set text to textfield android with one default value along with an additional value [duplicate]

I have strings defined in the usual strings.xml Resource file like this:
<string name="hello_world"> HELLO</string>
Is it possible to define format strings such as the one below
result_str = String.format("Amount: %.2f for %d days ", var1, var2);
in the strings.xml resource file?
I tried escaping the special characters but its not working.
You do not need to use formatted="false" in your XML. You just need to use fully qualified string format markers - %[POSITION]$[TYPE] (where [POSITION] is the attribute position and [TYPE] is the variable type), rather than the short versions, for example %s or %d.
Quote from Android Docs: String Formatting and Styling:
<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 integer. You can format the string with
arguments from your application like this:
Resources res = getResources();
String text = res.getString(R.string.welcome_messages, username, mailCount);
You should add formatted="false" to your string resource
Here is an example
In your strings.xml :
<string name="all" formatted="false">Amount: %.2f%n for %d days</string>
In your code:
yourTextView.setText(String.format(getString(R.string.all), 3.12, 2));
Inside file strings.xml define a String resource like this:
<string name="string_to_format">Amount: %1$f for %2$d days%3$s</string>
Inside your code (assume it inherits from Context) simply do the following:
String formattedString = getString(R.string.string_to_format, floatVar, decimalVar, stringVar);
(In comparison to the answer from LocalPCGuy or Giovanny Farto M. the String.format method is not needed.)
Quote from Android Docs:
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);
For me it worked like that in Kotlin:
my string.xml
<string name="price" formatted="false">Price:U$ %.2f%n</string>
my class.kt
var formatPrice: CharSequence? = null
var unitPrice = 9990
formatPrice = String.format(context.getString(R.string.price), unitPrice/100.0)
Log.d("Double_CharSequence", "$formatPrice")
D/Double_CharSequence: Price :U$ 99,90
For an even better result, we can do so
<string name="price_to_string">Price:U$ %1$s</string>
var formatPrice: CharSequence? = null
var unitPrice = 199990
val numberFormat = (unitPrice/100.0).toString()
formatPrice = String.format(context.getString(R.string.price_to_string), formatValue(numberFormat))
fun formatValue(value: String) :String{
val mDecimalFormat = DecimalFormat("###,###,##0.00")
val s1 = value.toDouble()
return mDecimalFormat.format(s1)
}
Log.d("Double_CharSequence", "$formatPrice")
D/Double_CharSequence: Price :U$ 1.999,90

Parameter in strings.xml does't work

I don't understand this anymore.
I try to write a TextView
android:text="#string/dbVer"
define in strings.xml
<string name="dbVer">db %1$s</string>
and in Activity
int dbTag = Integer.parseInt(yearDay.format(new Date(new File(databasePath + "/ean_database.db").lastModified())));
String dbVer = String.format(getString(R.string.dbVer), dbTag );
The TextView is still showing: db %1$s
The nearest answer I found: Are parameters in strings.xml possible? is similar but in fact something is wrong for me.
It looks like you are getting the result "db %1$s" because you are creating a string and assigning that as its value in the strings.xml file between these ><. What are you trying to have it show instead?
android:text="#string/dbVer"
This refers to your format string and displays the raw format string you're seeing.
int dbTag = Integer.parseInt(yearDay.format(new Date(new File(databasePath + "/ean_database.db").lastModified())));
String dbVer = String.format(getString(R.string.dbVer), dbTag );
This creates a new string dbVer using the format string from resources.
What is missing is that you need to set this new string as your TextView's text:
TextView tv = (TextView)findViewById(R.id.your_textview_id); // assuming an activity
tv.setText(dbVer);

Android TextView : "Do not concatenate text displayed with setText"

I am setting text using setText() by following way.
prodNameView.setText("" + name);
prodOriginalPriceView.setText("" + String.format(getString(R.string.string_product_rate_with_ruppe_sign), "" + new BigDecimal(price).setScale(2, RoundingMode.UP)));
In that First one is simple use and Second one is setting text with formatting text.
Android Studio is so much interesting, I used Menu Analyze -> Code Cleanup and i got suggestion on above two lines like.
Do not concatenate text displayed with setText. Use resource string
with placeholders. less... (Ctrl+F1)
When calling TextView#setText:
Never call Number#toString() to format numbers; it will not handle fraction separators and locale-specific digits properly. Consider
using String#format with proper format specifications (%d or %f)
instead.
Do not pass a string literal (e.g. "Hello") to display text. Hardcoded text can not be properly translated to other languages.
Consider using Android resource strings instead.
Do not build messages by concatenating text chunks. Such messages can not be properly translated.
What I can do for this? Anyone can help explain what the thing is and what should I do?
Resource has the get overloaded version of getString which takes a varargs of type Object: getString(int, java.lang.Object...). If you setup correctly your string in strings.xml, with the correct place holders, you can use this version to retrieve the formatted version of your final String. E.g.
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
using getString(R.string.welcome_message, "Test", 0);
android will return a String with
"Hello Test! you have 0 new messages"
About setText("" + name);
Your first Example, prodNameView.setText("" + name); doesn't make any sense to me. The TextView is able to handle null values. If name is null, no text will be drawn.
Don't get confused with %1$s and %2$d in the accepted answer.Here is a few extra information.
The format specifiers can be of the following syntax:
%[argument_index$]format_specifier
The optional argument_index is specified as a number ending with a “$” after the “%” and selects the specified argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.
The required format specifier is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.
Example
We will create the following formatted string where the gray parts are inserted programmatically.
Hello Test! you have 0 new messages
Your string resource:
< string name="welcome_messages">Hello, %1$s! You have %2$d new
messages< /string >
Do the string substitution as given below:
getString(R.string.welcome_message, "Test", 0);
Note:
%1$s will be substituted by the string "Test"
%2$d will be substituted by the string "0"
I ran into the same lint error message and solved it this way.
Initially my code was:
private void displayQuantity(int quantity) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + quantity);
}
I got the following error
Do not concatenate text displayed with setText. Use resource string with placeholders.
So, I added this to strings.xml
<string name="blank">%d</string>
Which is my initial "" + a placeholder for my number(quantity).
Note: My quantity variable was previously defined and is what I wanted to append to the string. My code as a result was
private void displayQuantity(int quantity) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(getString(R.string.blank, quantity));
}
After this, my error went away. The behavior in the app did not change and my quantity continued to display as I wanted it to now without a lint error.
Do not concatenate text inside your setText() method, Concatenate what ever you want in a String and put that String value inside your setText() method.
ex: correct way
int min = 120;
int sec = 200;
int hrs = 2;
String minutes = String.format("%02d", mins);
String seconds = String.format("%02d", secs);
String newTime = hrs+":"+minutes+":"+seconds;
text.setText(minutes);
Do not concatenate inside setText() like
text.setText(hrs+":"+String.format("%02d", mins)+":"+String.format("%02d", secs));
You should check this thread and use a placeholder like his one (not tested)
<string name="string_product_rate_with_ruppe_sign">Price : %1$d</string>
String text = String.format(getString(R.string.string_product_rate_with_ruppe_sign),new BigDecimal(price).setScale(2, RoundingMode.UP));
prodOriginalPriceView.setText(text);
Don't Mad, It's too Simple.
String firstname = firstname.getText().toString();
String result = "hi "+ firstname +" Welcome Here";
mytextview.setText(result);
the problem is because you are appending "" at the beginning of every string.
lint will scan arguments being passed to setText and will generate warnings, in your case following warning is relevant:
Do not build messages by
concatenating text chunks. Such messages can not be properly
translated.
as you are concatenating every string with "".
remove this concatenation as the arguments you are passing are already text. Also, you can use .toString() if at all required anywhere else instead of concatenating your string with ""
I fixed it by using String.format
befor :
textViewAddress.setText("Address"+address+"\n"+"nCountry"+"\n"+"City"+"city"+"\n"+"State"+"state")
after :
textViewAddress.setText(
String.format("Address:%s\nCountry:%s\nCity:%s\nState:%s", address, country, city, state));
You can use this , it works for me
title.setText(MessageFormat.format("{0} {1}", itemList.get(position).getOppName(), itemList.get(position).getBatchNum()));
If you don't need to support i18n, you can disable this lint check in Android Studio
File -> Settings -> Editor -> Inspections -> Android -> Lint -> TextView Internationalization(uncheck this)
prodNameView.setText("" + name); //this produce lint error
val nameStr="" + name;//workaround for quick warning fix require rebuild
prodNameView.setText(nameStr);
I know I am super late for answering this but I think you can store the data in a varible first then you can provide the variable name. eg:-
// Java syntax
String a = ("" + name);
String b = "" + String.format(getString(R.string.string_product_rate_with_ruppe_sign);
String c = "" + new BigDecimal(price).setScale(2, RoundingMode.UP));
prodNameView.setText(a);
prodOriginalPriceView.setText(b, c);
if it is textView you can use like that : myTextView.text = ("Hello World")
in editText you can use myTextView.setText("Hello World")

How can I add margin or padding between 2 lines in spannable?

I am using a spannable to create a string in 2 lines where each line has different font.
It works fine except one thing.
How can I add some margin between the first string with the second string?
So I do:
String a = "First line";
String b = "Second line";
SpannableString spannableName = new SpannableString(a + "\n" + b);
spannableName.setSpan(new TextAppearanceSpan(activity, R.style.my_style),0,a.length(), 0);
and my_style
<style name="my_style">
<item name="android:layout_marginBottom">100dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:textSize">#dimen/size</item>
<item name="android:textColor">#color/mycolor</item>
</style>
What am I doing wrong?
I want to have some padding/margin between a and b.
The paddings/margins in my style are big because I wasn't sure if they are applied or not (test numbers)
Here is how you can do it with RelativeSizeSpan:
String a = "First line";
String b = "Second line";
SpannableString spannableName = new SpannableString(a + "\n" + b);
spannableName.setSpan(new TextAppearanceSpan(activity, R.style.my_style),0,a.length(), 0);
spannableName.setSpan(new RelativeSizeSpan(1.5f), a.length(), a.length() + 1, 0);
RelativeSizeSpan will make the "\n" character 1.5 bigger than the "a" and "b" string.
That has to be a property of the UI control you are using.
I assume you are setting the text into a TextView. You can use textView.setLineSpacing(addvalue, multiplyValue). This will add addValue units to the spacing and multiply it multiplyValue times.
Similarly, if you want it set through style you can add these values to your style:
<item name="android:lineSpacingExtra">SoemDPValue</item>
<item name="android:lineSpacingMultiplier">someNumber</item>

Categories

Resources