selva
selva

Reputation: 1503

How to concat text in Android TextView?

I want to concat two text and display with single TextView in Android. I have tried following type. It displays in logcat only, but it isn't concat in XML textview.

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 );

Anything wrong here?

Upvotes: 4

Views: 21177

Answers (5)

Rosniel Isse
Rosniel Isse

Reputation: 21

What you intend to do is not as complicated as you see, it is true that the result you are getting is not what you want, looking at your code I can say that not even the program should compile because it must be generating a syntax error. This is because you cannot concatenate the text using setText. The solution can be the following.

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 is as follows:

My ID number is: 8945213624

There is nothing wrong with your code, you just have to make proper use of the elements of concatenation, in this case that I show I use append to be able to concatenate the elements.

Upvotes: 2

Mario Jorge
Mario Jorge

Reputation: 1

txtConcat = (TextView)findViewById(R.id.txt);

Shouldn't it be: txtConcat = (TextView)findViewById(R.id.heizgriffe);

Upvotes: -1

biraj patel
biraj patel

Reputation: 36

txtConcat = (TextView)findViewById(R.id.txt); // initialize
String hub="Hubraum:";
String str= ItemList.getTxt(); // fetching from webservice
txtConcat .setText(hub + str );

Upvotes: 2

jose Louren&#231;o
jose Louren&#231;o

Reputation: 1

String str= (String) ItemList.getTxt();

try this

Upvotes: -1

Madhuri
Madhuri

Reputation: 368

Try This Answer........

String hub="Hubraum:";
String str= hub+ItemList.getTxt(); // fetcting from webservice
txtConcat.setText(str );

Upvotes: -1

Related Questions