Add more than one String to a TextView

18,239

Solution 1

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

Solution 2

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"
Share:
18,239
Javi
Author by

Javi

Updated on June 08, 2022

Comments

  • Javi
    Javi almost 2 years

    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