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 typeObject
: 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.using
getString(R.string.welcome_message, "Test", 0);
android will return a String with
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.I ran into the same lint error message and solved it this way.
Initially my code was:
I got the following error
So, I added this to
strings.xml
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 wasAfter 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.
You should check this thread and use a placeholder like his one (not tested)
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: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""
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
Do not concatenate inside setText() like
Don't get confused with %1$s and %2$d in the accepted answer.Here is a few extra information.
Example
We will create the following formatted string where the gray parts are inserted programmatically.
Your
string resource
:Do the
string substitution
as given below:Note: