Android Textview:"Do Not Concatenate Text Displayed with Settext"

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.

do not concatenate text displayed with settext

You can format your string like this

double latitude = gpsTracker.getLatitude();
double longitude = gpsTracker.getLongitude();
String text = String.format("%1$2f / %2$2f", latitude, longitude);
textviewGPSLocation.setText(text);

Kotlin Do not concatenate text displayed with setText. Use resource string with placeholders.

Create a string resource like following

 <string name="station_counter_text">Station %1$d</string>

Then from Activity/Fragment use like following

for(i in 1..10)
frame.stationTextView.text = getString(R.string. station_counter_text,i)

Android Kotlin Do not concatenate test displayed with setText. Use resource string with placeholders

You have to pass format arguments to the getString method:

pTxt.text = context.getString(R.string.displayPriceMsg, price)

I want to concat two strings for a TextView in android, Data Binding Api

concate it with grave accent (`)

android:text="@{`Hello ` + user.firstName}"/>

You can concat it in multiple ways, check it here concat-two-strings-in-textview-using-databinding



Related Topics



Leave a reply



Submit