I have a TextView which has a hardcoded string and I have a dynamic variable that I want to put at the end of this string. This is my code:
android:text= "@{@string/generic_name(user.name)}"
Just make string resource like this.
Hello %s
android:text="@{`Hello ` + user.name}"/>
This is useful when you need hardcoded append like + for phone number.
String
's concat methodandroid:text="@{user.firstName.concat(@string/space).concat(user.lastName)}"
Here space
is an html entity which is placed inside strings.xml
. Because XML
does not accept Html entities or special characters directly. (Link Html Entities)
\u0020
String.format()
android:text= "@{String.format(@string/Hello, user.name)}"
you have to import String class in layout in this type.
android:text="@{@string/generic_name(user.firstName,user.lastName)}"
In this case put a string resource in strings.xml
%1$s, %2$s
There can be many other ways, choose one you need.