5

Is it possible trim() method is work on two ways data binding, when getting info from XML. If yes how?

android:text='@={contact.contactDetails.name}'
dur
  • 15,689
  • 25
  • 79
  • 125
Lovekesh
  • 125
  • 11

3 Answers3

3

You can trim it in getter/setter method of you model class.

public class UserModel {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name.trim();
    }
}

android get it with binding.getUser().getName() , even you can use trim() in getName() both will work same.

Ravi
  • 34,851
  • 21
  • 122
  • 183
0

I had the same problem. For me, neither trim() not trim in layout didn't work. My decision was create new method, that replaced all spaces at string.

Utils.class
public static String trim(String string) {
        return string.replaceAll(" ", "");
    }

And then in layout:

android:text="@{Utils.trim(contact.contactDetails.name)}"
Jane
  • 61
  • 7
-1

This is also possible:

android:text='@={contact.contactDetails.name.trim}'

Micer
  • 8,731
  • 3
  • 79
  • 73