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

I set the text using setText () as follows.

prodNameView.setText("" + name); prodOriginalPriceView.setText("" + String.format(getString(R.string.string_product_rate_with_ruppe_sign), "" + new BigDecimal(price).setScale(2, RoundingMode.UP))); 

In this first one is a simple use and the second is customizable text with formatting text.

Android Studio is so interesting, I used Menu Analyze -> Code Cleanup , and I got a suggestion over two lines like.

enter image description here

Do not combine text displayed with setText. Use a resource string with placeholders. less ... (Ctrl + F1)

When calling TextView # setText:

  • Never call Number # toString () to format numbers; it will not properly handle fractional separators and corresponding language values. Consider using String # format with appropriate format specifications (% d or% f) instead.
  • Do not pass a string literal (for example, "Hello") to display text. Hard-coded text cannot be translated into other languages. Instead, consider using Android resource strings.
  • Do not create messages by combining text fragments. Such messages cannot be properly translated.

What can I do for this? Can anyone help explain what this is and what should I do?

+118
android string-concatenation textview concatenation android-textview
Oct. 16 '15 at 7:32
source share
5 answers

The resource has an overloaded version of getString that accepts varargs type Object : getString (int, java.lang.Object ...) . If you correctly configured your line in the strings.xml file with the correct place holders, you can use this version to get formatted version of your last line. For example.

 <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 is prodNameView.setText("" + name); doesn't make any sense to me. TextView is capable of handling null values. If the name is null, the text will not be drawn.

+253
Oct 16 '15 at 7:41
source share

Do not confuse with % 1 $ s and % 2 $ d in the accepted answer. Here are some additional information.

  • Format specifiers can have the following syntax:

% [ argument_index $] format_specifier

  • The optional index_ argument is specified as a number ending with "$" after "%" and selects the specified argument from the argument list. The first argument refers to $ 1 , the second refers to $ 2 , etc.
  • Required format specifier is a character indicating how the argument is formatted. The set of valid conversions for this argument depends on the data type .

Example

We will create the following formatted line in which the gray parts will be inserted programmatically.

Hi Test ! you have 0 new posts

Your string resource :

<string name = "welcome_messages"> Hello, %1$s ! You have %2$d new Messages </string>

Make a string substitution as shown below:

getString (R.string.welcome_message, "Test" , 0 );

Note:

  • % 1 $ s will be a substring on the line "Test"
  • % 2 $ d will be a substring on the string "0"
+23
Nov 19 '17 at 17:53 on
source share

I came across the same error message and solved it this way.

My code initially looked like:

 private void displayQuantity(int quantity) { TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view); quantityTextView.setText("" + quantity); } 

I got the error message "Do not concatenate the text displayed with setText. Use a resource string with placeholders." So

I added this to strings.xml

 <string name="blank">%d</string> 

What is my initial "+" for my number (quantity).

note: my quantity variable was previously defined and is what I wanted to add to the line. In java code I wrote.

 private void displayQuantity(int quantity) { TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view); quantityTextView.setText(getString(R.string.blank, quantity)); } 

and my mistake has disappeared. The behavior in the application did not change, and my quantity continued to be displayed as I wanted it, without a lint error.

+13
Jan 28 '16 at 5:46
source share

You should check this thread and use a placeholder similar to it (not verified)

 <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); 
+8
Oct 16 '15 at 7:44
source share

the problem is that you add a "" at the beginning of each line.

lint will check the arguments passed to setText and it will generate warnings, in your case the following warning is the following:

Do not create messages combining text fragments. Such messages may not be properly translated.

since you concatenate each line with "" .

remove this concatenation as the arguments you pass are already texts. Alternatively, you can use .toString() if you don't need it anywhere at all instead of concatenating your string with ""

+4
Oct. 16 '15 at 8:02
source share



All Articles