for the new line in TextView just add \n in middle of your text it works..
Two-way Data Binding is a technique of binding your objects to your XML layouts so that the layout can send data to your binding object.
The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. Layouts are often defined in activities with code that calls UI framework methods.
concate it with grave accent (`)
android:text="@{`Hello ` + user.firstName}"/>
You can concat it in multiple ways, check it here concat-two-strings-in-textview-using-databinding
This is already answered by @GeorgeMount in comments to one of the solution. Which to me looks like the best solution so far here.
android:text="@{@string/location(user.city,user.state)}"
in your strings.xml
<string name="location">%1$s, %2$s</string>
android:text= "@{@string/generic_name(user.name)}"
Just make string resource like this.
<string name="generic_name">Hello %s</string>
android:text="@{`Hello ` + user.name}"/>
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)
<string name="space">\u0020</string>
String.format()
android:text= "@{String.format(@string/hello, user.name)}"
you have to import String class in layout in this type.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="String" />
</data>
<TextView
android:text= "@{String.format(@string/hello, user.name)}"
... >
</TextView>
</layout>
android:text="@{@string/generic_name(user.firstName,user.lastName)}"
In this case put a string resource in strings.xml
<string name="generic_name">%1$s, %2$s</string>
There can be many other ways, choose one you need.
Since xml supports single quotes for values of attribute, you can also do this :
android:text='@{"Hello "+user.firstName}'
There are two ways.
First Solution
concat with grave accent (`)
android:text="@{`Hello ` + user.firstName}"/>
Second Solution
Declare Your string in strings.xml
like "Hello %1$s , (whatever you want to add then add here)"
.
amd use String.format(stringResource, upsatename);
In case of static string and other dynamic you can use this
android:text="@{`Hello ` + user.firstName}"/>
In case of dynamic data you can use this.
android:text='@{user.firstName+" "+user.lastName}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With