code_legend
code_legend

Reputation: 3285

Add multiple text to a TextView

I have created a TextView with similar details:

 <TextView
            android:id="@+id/tAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tHeadline"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="4dp"
            android:gravity="center" 
            android:hint="Age"
                        android:textSize="18sp"

            android:textColor="#000" />

I would like to set text for multiple strings. such as

mUserAgeRetrieved.setText("Age:");

        mUserAgeRetrieved.setText(String.valueOf(objects.get(i).getInt("UserAge")));

Hence, the result of the above would be Age: (age that is generated from Parse).

Another Instance

mUserHeadlineRetrieved = (TextView) findViewById(R.id.tHeadline);
mUserAgeRetrieved.setText("Headline:");

                mUserHeadlineRetrieved.setText(objects.get(i).get("Headline")
                        .toString());

Hence, the result of the above would be Headline: (Headline that is generated from Parse). The problem I have is that It chose the last setText, and does not combined both of them.

Lets say I wanted to have one of them bold like Age: (bold) 25 (non-bold)

If you need any clarification, let me know.

Upvotes: 1

Views: 3256

Answers (3)

arlistan
arlistan

Reputation: 741

You can set both texts to a single string and then set it, like this:

    mUserAgeRetrieved.setText(Html.fromHtml("<b>" + "Age:" + "</b>" + String.valueOf(objects.get(i).getInt("UserAge"))));

Upvotes: 2

Alex K
Alex K

Reputation: 8338

Of course it doesn't! setText sets new text.

The solution is very simple.

textView.setText("This: ");
textView.append("works");

textView should say "This: works"

Hope this helps,

good luck :)

Upvotes: 4

Stephen
Stephen

Reputation: 4249

mUserAgeRetrieved.setText("Age: " + String.valueOf(objects.get(i).getInt("UserAge")));

or

mUserHeadlineRetrieved.setText("Headline: " + objects.get(i).get("Headline").toString());

You just need to use regular string concatenation.

Upvotes: 2

Related Questions