2

I can't find attribute android:onTextChanged in EditText when implement data-binding to my fragment xml. I have searched for the answer, and this one said it is supported out of the box. I have set

data-binding { enable = true }

in my build.gradle, and my current build gradle is 3.5.3.

Here is a part of my xml:

<layout>

    <data>

        <variable
            name="vm"
            type=".ViewModel" />
    </data>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".screens.hangouts.create_hangout_new.Step1FragmentNew">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_category"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tv_hangout_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/rv_category" />

            <EditText
                android:id="@+id/et_hangout_name"
                android:background="@drawable/bkg_stroke_red"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                app:layout_constraintTop_toBottomOf="@id/tv_hangout_name" />
...
Dung Nguyen
  • 91
  • 2
  • 5

5 Answers5

0

You can use

 android:afterTextChanged="@{(editable)->viewModel.afterEmailTextChanged(editable)}"

set Function in your viewmodel

public void afterEmailTextChanged(CharSequence s) {
    user.setEmail(s.toString());
}
  • 2
    The problem is I can't not find any of those attribute in `EditText`, including `android:onTextChanged`, `android:beforeTextChanged`, `android:afterTextChanged` – Dung Nguyen Dec 31 '19 at 02:55
0

In your xml file:

<EditText
   android:id="@+id/et"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:textChangedListener="@{model.textWatcher}"  />  

In your code like this in your model class :

public class Model{
private TextWatcher textWatcher;

public Model(){
        this.textWatcher= getTextWatcherIns();
}

private TextWatcher getTextWatcherIns() {
        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //do some thing
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //do some thing

            }

            @Override
            public void afterTextChanged(Editable s) {
                //do some thing
            }
        };
    }

    public TextWatcher getTextWatcher() {
        return textWatcher;
    }

    public void setTextWatcher(TextWatcher textWatcher) {
        this.textWatcher = textWatcher;
    }

    @BindingAdapter("textChangedListener")
    public static void bindTextWatcher(EditText editText, TextWatcher textWatcher) {
        editText.addTextChangedListener(textWatcher);
    }
}

For more information about it read this Doc. Also include in your answer app:textChangedListener in EditText and try.

I hope it'll help you...!

Viral Patel
  • 1,296
  • 1
  • 11
  • 24
  • Thank you for your answer, but this does not resolve my problem. I can't find the attribute `onTextChanged` in `EditText` even they say it's supported out of the box. – Dung Nguyen Dec 31 '19 at 02:53
0

You can use 2-way binding if you're using data binding like below,

<EditText
            android:id="@+id/et_hangout_name"
            android:background="@drawable/bkg_stroke_red"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:text="@={viewmodel.textChangedMutableLiveData}"
            app:layout_constraintTop_toBottomOf="@id/tv_hangout_name" />

Then in your viewmodel,

var textChangedMutableLiveData = MutableLiveData<String>()

var textChangedLiveData = textChangedMutableLiveData.map {
    // here you get the changed text every time user types something. now you can observe textChangedLiveData in your activity and do whatever you wanna do when text changes. 
}
Abhishek Kumar
  • 4,532
  • 5
  • 31
  • 53
0

You need to define a BindingAdapter in order to use it in XML like android:afterTextChanged

Define somewhere in your project

@BindingAdapter("android:afterTextChanged")
public static void setListener(TextView view, TextViewBindingAdapter.AfterTextChanged after) {
        setListener(view,after);
    }

The code is taken reference here: this link

ken
  • 2,426
  • 5
  • 43
  • 98
0

This is because probably you're using string instead of char sequence.

For eg if you want to enable a button on a edittext filling up do this

In viewmodel


val enableButton = MutableLiveData<Boolean>(false)

  fun onTextChange(value: CharSequence) {
        enableButton.value = value.isNotEmpty()
    }

and in XML

  <EditText
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:onTextChanged="@{(text, start, before, count) -> viewModel.onTextChange(text)}"
            app:layout_constraintEnd_toStartOf="@+id/materialButton"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

      <Button
            android:id="@+id/materialButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginEnd="8dp"
            android:enabled="@{viewModel.enableButton}"
            app:layout_constraintBottom_toBottomOf="@+id/editText"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/editText" />

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31