0

I am trying to do a two way databinding on android application. I have set up the application to bind the data to the UI but don't know how i can update the data model when edittext text changed.

I think of creating extended edittext which override the focus and textchanged so it can detect the text changed and focus for validation. But how can I know which data object am I dealing with.

I tried using app:addtextchangelistener by passing the function which return a textwatcher in the layout.xml but I received nullpointerexception.

Does anyone know how to do Two-way databinding on android?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
LittleFunny
  • 8,155
  • 15
  • 87
  • 198

2 Answers2

0

I would add a custom BindingAdapter and set the onFocus/textchanged listener directly on the EditText

  @BindingAdapter("your_custom_attribute_name")
  public static void briansSetEditTextListener(EditText editText, Boolean clear){
    if (editText != null && clear) {
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View text, boolean hasFocus) {
                ((EditText)text).setHint("");
            }
        });
    }
}
stoyan
  • 166
  • 1
  • 3
  • can you give me an example of what the xml file would look like that uses this ? or at least what the edittext would look like ? – j2emanue Jan 28 '16 at 15:29
0

You can implement onTextChangeMethod in your data Object

 public void onTextChanged(CharSequence newText, int start, int before, int count) {
     setStringField(newText.toString());
 }

and add

<EditText
        ...
        android:onTextChanged="@{data.onTextChanged}" />

So, your data will be updated every time your text changed. In addition, if you display yourField on UI it should be ObservableField.

public class Data {
    private ObservableField<String> stringField = new ObservableField<>();

    public void setStringField(String changingText) {
        stringField.set(changingText);
    }

    public ObservableField<String> getStringField() {
        return stringField;
    }

    public void onTextChanged(CharSequence newText, int start, int before, int count) {
        setStringField(newText.toString());
    }
}

All layout looks like this:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="data"
            type="your.package.Data" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onTextChanged="@{data.onTextChanged}" />

         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.stringField}" />
    </LinearLayout>
</layout>
lukjar
  • 7,220
  • 2
  • 31
  • 40
  • I have it working.. but I ended up coding in a normal way than using databinding... Somehow I think databinding in android it's not so feasible. – LittleFunny Mar 13 '16 at 22:03