0

in my simple application i'm trying to implementing TextWatcher for some EditText that i have in layout,

i want to support getting default current edittext value which i set into that by code and support afterTextChanged, for implementing that i have this EditText into layout

reference

<EditText
    android:id="@+id/instagram_page_name"
    style="@style/Base.TextAppearance.AppCompat.Caption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:textChangedListener="@{viewModel.usernameWatcher}"/>

EditTextBindingAdapters binding Adapter class:

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

and my ViewModel:

public class LoginViewModel extends ViewModel {
    private User user;
    private LoginResultCallbacks loginResultCallbacks;

    public LoginViewModel(LoginResultCallbacks loginResultCallbacks) {
        this.loginResultCallbacks = loginResultCallbacks;
        this.user = new User();
    }

}

PROBLEM 1:

on EditTextBindingAdapters class addTextChangedWatcher is uknown

PROBLEM 2:

how can i coding in ViewModel to use EditTextBindingAdapters class?

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • Use the function addChangedTextListener(textWatcher) instead of addTextChangedWatcher(textWatcher). You should'nt EditTextBindingAdapters in your ViewModel, the purpose of ViewModel is to retain your data and to allow you to access them from your view whenever you need it – Robert LaFondue Apr 12 '19 at 15:02
  • @RobertLaFondue `EditTextBindingAdapters` is on another place as class, how can i use that into `ViewModel`? – DolDurma Apr 12 '19 at 15:53
  • You should use you ViewModel in the fragment/activity where you use your EditTextBindingAdapters, not the opposite – Robert LaFondue Apr 15 '19 at 07:46

0 Answers0