Search code examples
androidxmlandroid-layoutandroid-textinputlayout

Do not move hint to the top when calling setError() of TextInputLayout


I have a layout file with a TextInputLayout:

<android.support.design.widget.TextInputLayout
    android:id="@+id/fullname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true"
    app:errorTextAppearance="@style/Error">

    <EditText
        android:id="@+id/fullnameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Full Name"
        android:inputType="textEmailAddress"
        android:padding="26dp"/>
</android.support.design.widget.TextInputLayout>

When I call the setError() method on the TextInputLayout the hint goes to the top as if the TextInputLayout would have focus.

I would like to leave the hint untouched while setting the error. It should look something like this:

design

Any ideas how to build this layout?


Solution

  • you can do it with alternative way , you can call setError() on OnCreate()

     mTextInputLayout = findViewById(R.id.fullname);
     mTextInputLayout.setError("Enter Your Full Name");
    

    and when you want to show the Error do this

    mTextInputLayout.setErrorEnabled(true);
    

    and this will fix your problem with the hint

       if (mTextInputLayout.isErrorEnabled()){
             mTextInputLayout.setHintEnabled(false);
          }else {
             mTextInputLayout.setHintEnabled(true);
          }