Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

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?

like image 301
Pratik Butani Avatar asked Oct 14 '22 16:10

Pratik Butani


People also ask

Do not concatenate text displayed with settext?

Do not concatenate text displayed with setText. Use resource string with placeholders. Never call Number#toString () to format numbers; locale-specific digits properly. it will not handle fraction separators and locale-specific digits properly. Consider using String#format with proper format specifications (%d or %f) instead.

What should not be used with settext()?

Do not concatenate text displayed with setText. Do not concatenate text displayed with setText. Use resource string with placeholders. Never call Number#toString () to format numbers; locale-specific digits properly. it will not handle fraction separators and locale-specific digits properly.

What should not be used to display text in Android?

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. Do not build messages by concatenating text chunks. Such messages can not be properly translated.

What does it mean to concatenate text?

To concatenate is to put two or more things together. For example, consider three text stings: apple, cat, and wallet. A simple concatenation of those three strings would get us applecatwallet.


2 Answers

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.

like image 395
Blackbelt Avatar answered Oct 17 '22 05:10

Blackbelt


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

  1. 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.
  2. 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"
like image 54
Rissmon Suresh Avatar answered Oct 17 '22 06:10

Rissmon Suresh