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
Firebase Admin Sdk for Android, Methods Not Found
How to Detect Power Connected State
Android Device Gps On/Off Programmatically
Upload Video to Facebook in Android
How to Serialize My User Class for the Firebase Database and Fix This Error
How to Record Microphone to More Compressed Format During Webrtc Call on Android
Adb Connection by Wifi Getting Killed When a New Usb Attached/Detached
Build Unsigned APK File with Android Studio
What Does It Mean to Inflate a View from an Xml File
How to Round an Image with Glide Library
Sign APK Without Putting Keystore Info in Build.Gradle
How to Make a Actionbar Like Google Play That Fades in When Scrolling
Android Destroying Activities, Killing Processes
Android: What Are the Recommended Configurations for Proguard
How to Get Activity's Content View
How to Check an Android Device Is Hdpi Screen or Mdpi Screen