Concat Two Strings In TextView Using DataBinding
Sometimes we might need a functionality where we needs to concat two strings in xml only. So here are the different ways of how can we do it using databinding.
1. Using grave accent(`)
1 |
android:text="@{`Hello ` + user.firstName}" |
2. Using strings.xml
1 |
android:text="@{@string/location(user.city,user.state)}" |
and in your string.xml file
1 |
<string name="location">%1$s, %2$s</string> |
3. Using concat()
1 |
android:text="@{user.firstName.concat(@string/space).concat(user.lastName)}" |
4. Using string.format()
1 |
android:text= "@{String.format(@string/Hello, user.name)}" |
For above approach you need to import String class.
1 2 3 |
<data> <import type="String" /> </data> |