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?
String
intosetText()
. Ex:setText(name)
in stead ofsetText("" + name)
. Because if you concatenate text, it will not be translated like you use Hardcoded text as the message notify – Mr NeoNPE
ifname
isNULL
– Pratik Butaniname
is notNULL
before usingsetText()
function. – Mr Neo<string name="string_product_rate_with_ruppe_sign">Something %1$d</string>
And in your java code you do something like this:prodOriginalPriceView.setText(getString(R.string.string_product_rate_with_ruppe_sign), price);
(you can do the formatting in the xml file: [developer.android.com/guide/topics/resources/… – CodeBreakers