'I have an Custom Edittext and im using viewmodel but text input doesnt get updated in viewmodel?

My normal edittext which works:

<EditText
                    android:id="@+id/emailEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dimen_8_dp"
                    android:hint="Email"
                    android:imeOptions="actionNext"
                    android:inputType="textEmailAddress"
                    android:text="@={viewModel.emailId}"
                    android:textSize="@dimen/font_size_default_input" />

Now Im using:

 <myapp.app.widgets.edittext.MyCustomEditText
                    android:id="@+id/panEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dimen_8_dp"
                    android:hint="PAN number"
                    android:imeOptions="actionNext"
                    android:inputType="textCapCharacters"
                    android:maxLength="10"
                    android:text="@{viewModel.pan}"
                    android:textSize="@dimen/font_size_default_input" />

Now this doesnt work.

my method

!viewModel.isPanValid() -> binding.panEditText.setError(viewModel.panError.value)

doesnt work but for email it works what to do?

the MyCustomEditText is just en extended class

public class MyCustomEditText extends LinearLayout implements View.OnFocusChangeListener, TextWatcher, TextView.OnEditorActionListener, View.OnClickListener {

It includes just a UI stuff and on focus listener etc



Solution 1:[1]

setError is a function of EditText. Your MyCustomEditText is not an EditText but a LinearLayout so you can't call setError on it because it simply doesn't exist for that class. I don't know the rest of MyCustomEditText but I assume it has an EditText inside it and you might have a reference to it, say editTextInThisClass. If not you will need to create one. Then you could write this in your MyCustomEditText:

public void setError(CharSequence error) {
    editTextInThisClass.setError(error)
}

but I wonder if you actually intended for it to be a LinearLayout. If not you should just change LinearLayout to EditText

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivo Beckers