I want to concat two strings for a TextView in android, Data Binding Api

前端 未结 10 774
太阳男子
太阳男子 2020-12-23 14:04

Im using DataBinding Api for setting the views in android layouts. Here is my layout.

layout.xml



        
相关标签:
10条回答
  • 2020-12-23 14:45

    Since xml supports single quotes for values of attribute, you can also do this :

    android:text='@{"Hello "+user.firstName}'
    
    0 讨论(0)
  • 2020-12-23 14:49

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

    0 讨论(0)
  • 2020-12-23 14:50

    Many ways to concat strings

    1. Using string resource (Most preferable because of localization)

    android:text= "@{@string/generic_name(user.name)}"

    Just make string resource like this.

    <string name="generic_name">Hello %s</string>
    

    2. Hard coded concat

    android:text="@{`Hello ` + user.name}"/>
    

    3. Using String's concat method

    android: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>
    

    4. Using 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>
    

    5. Another method

    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.

    0 讨论(0)
  • 2020-12-23 14:53

    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>
    
    0 讨论(0)
  • 2020-12-23 14:55

    To do a concat in xml layout:

    <data>
    
    /*This is used for android view*/
    <import type="android.view.View" />
    
    /*This is used for android resources*/
    <import type="com.myapp.R" />
    
    /*This is app context*/
    <variable
        name="context"
        type="android.content.Context" />
    
    /*This is used for model data*/
    <variable
        name="item"
        type="com.myapp.models.Chapter" />
    </data>
    
    android:text="@{item.serialNo.concat(@string/space).concat(item.title)}"
    

    In strings.xml I have added code for blank space:

    <string name="space">\u0020</string>
    
    0 讨论(0)
  • 2020-12-23 14:59

    Probably late to the party: Below code will also work

    android:text='@{@string/hello.concat(user.firstName)}'

    0 讨论(0)
提交回复
热议问题