How to combine text in Android TextView?
I want to combine two texts and display with one TextView in Android. I tried the following type. It only displays in logcat, not XML.
Here is my code:
<TextView
android:id="@+id/heizgriffe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heizgriffe"
android:textColor="#000"
android:textSize="12dp"
android:textStyle="bold"
android:layout_below="@+id/txt"/>
in Java class:
txtConcat = (TextView)findViewById(R.id.txt);
String hub="Hubraum:";
String str= ItemList.getTxt(); // fetting from webservice
txtConcat .setText(hub + str );
Is there something wrong here?
source to share
What you intend to do is not as difficult as you can see, it is true that the result you are getting is not what you want, by looking at your code, I can tell that even the program should not compile because it should generate a syntax error. This is because you cannot concatenate text using setText. The solution could be as follows.
XML file:
<TextView
android:id="@+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
Java class:
public TextView tvInfo;
private java.lang.CharSequence ID = "8945213624";
TextView tvInfo = (TextView)findViewById(R.id.tvInfo);
tvInfo.setText("My ID number is: ");
tvInfo.append(ID);
The result looks like this:
My ID: 8945213624
There is nothing wrong with your code, you just need to use the concatenation elements correctly, in which case I will show that I am using append to be able to concatenate the elements.
source to share