Android TextView : “Do not concatenate text displayed with setText”

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.

Leave a Comment