Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add more than one String to a TextView

I have some strings in a xml, for example

<string name="String1">My String 1</string>
<string name="String2">My string 2</string>

and I want to show in the activity something like My String 1: My string 2

Is it possible to add to the same TextView more than a

<TextView android:text="@string/String1"/>
<TextView android:text=": "/>
<TextView android:text="@string/String2"/>

The problem of this is that if you insert them inside a TableLayout they are considered as cells and the ":" symbol is not written next to String1 (it's written in the middle of both Strings).

Is it possible to join the string in just one TextView in the xml code (without doing it programmatically in Java)? I mean is there any syntax to append strings something like

<TextView android:text="@string/String1+:+@string/String2"/>

Thanks

like image 393
Javi Avatar asked Nov 17 '10 17:11

Javi


2 Answers

You cant do it directly in the xml, this is the best you can do:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>


Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
like image 65
blindstuff Avatar answered Nov 02 '22 08:11

blindstuff


I'm not 100%, but I dont think its possible to have more than one android:text per TextView.

If you are going to have three TextViews, you need to add something like to String2(or whatever view has the ":":

android:layout_toRightOf="@id/string1"
like image 1
James Avatar answered Nov 02 '22 06:11

James