Abdul
Abdul

Reputation: 238

i am having problem to add comma with space in data-binding XML layout in android

Databinding is the most commonly used to bind UI views in layout to data source. that is why I decided to use this approach.

Actually, I am to concatenate to of the values from data source and to be shown in view. concatenation is not a big deal I just added "+" operator between the values or variables of the data source.

The problem is how to add space between two of them.

concatenating two of the values by "+" operator was working fine.


  <TextView
                    android:id="@+id/location"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="@sample/title"
                    android:text="@{item.location.get(0).getlocation_text + item.location.get(0).city}"
                    android:maxLines="2"
                    android:ellipsize="end"
                    android:gravity="start"
                    android:textAppearance="@style/TextDesc2"
                    card_view:layout_constraintTop_toBottomOf="@+id/title"
                    card_view:layout_constraintStart_toStartOf="parent"
                    card_view:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginTop="8dp" card_view:layout_constraintHorizontal_bias="0.0"/>

I need the result as "locatin_text, city"

but now I am getting "location_textcity"

Upvotes: 1

Views: 1101

Answers (2)

kevz
kevz

Reputation: 2737

You can use double quotes with back quotes or single quotes with double quotes. Check this Android developer document reference.

1st way

android:text="@{item.location.get(0).getlocation_text + `, ` +  item.location.get(0).city}"

2nd way

android:text='@{item.location.get(0).getlocation_text + ", " +  item.location.get(0).city}'

Upvotes: 4

Haresh Ramani
Haresh Ramani

Reputation: 390

try this

android:text='@{String.format("%s %s", item.location.get(0).getlocation_text, item.location.get(0).city)}'

Upvotes: 1

Related Questions