Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a string with space by dataBinding

Tags:

android

I want to make the app show me the string like "24 girls"; but when I use dataBinding , the space in string can't be shown,the string change like "24girls".

This is my code: enter image description here

like image 992
Lewis Avatar asked May 19 '16 06:05

Lewis


People also ask

Can we use DataBinding and ViewBinding together?

You can't use them togheter in the same layout. ViewBinding is a subset of what DataBinding can do and should be used if you want to replace libraries like ButterKnife , KotterKnife or KAE (Kotlin Android Extensions) but don't want to invest in databinding refactoring.

How do you pass context in DataBinding?

You just have to import <import type="android. content. Context" /> in your xml data imports. Then, you can access context for your databinding simply with context .


1 Answers

I would suggest you to use plurals for this. In your strings.xml add this:

<plurals name="scores">
    <item quantity="one">%d Girl</item>
    <item quantity="other">%d Girls</item>
</plurals>

and in your layout file

android:text="@{@plurals/scores(setScore.score, setScore.score)}"

The first setScore.score is used to decide which string should be used from the plurals. And the second setScore.score is for argument %d we mentioned in plurals.

like image 109
Rehan Avatar answered Sep 23 '22 14:09

Rehan