Search code examples
androidandroid-layoutandroid-edittextandroid-textinputlayoutandroid-textinputedittext

android textInputEditText with custom background is not working ok


i'm using textInputEditText inside textInputLayout i had to set background for my editText to achieve a bordered view for my editText. but when i call setError() on my textInputLayout the entire editText color changes to red. but i want to change only the color of error text, not the entire view.

before setting error :

screen shot

after setting error :

screen shot

and here is my xml code :

<android.support.design.widget.TextInputLayout
            android:layout_alignParentTop="true"
            android:id="@+id/ex_pass_holder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="false"
            android:gravity="right">
            <android.support.design.widget.TextInputEditText
                android:id="@+id/ex_pass_et"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:hint="رمز عبور فعلی"
                android:inputType="textPassword"
                android:textColor="#000"
                android:textSize="15sp"
                android:gravity="right|center"
                android:background="@drawable/edittext_bg"
                android:padding="8dp"
                />
        </android.support.design.widget.TextInputLayout>

please help me, what am i doing wrong?


Solution

  • i fixed this by extending TextInputLayout and overriding some methods

    public class CustomTextInputLayout extends TextInputLayout {
    
        public CustomTextInputLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
    
    
        @Override
        public void setError(@Nullable CharSequence error) {
            super.setError(error);
            try {
                EditText et = getEditText();
                Drawable editTextBackground = et.getBackground();
                editTextBackground.clearColorFilter();
    
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        @Override
        protected void drawableStateChanged() {
            super.drawableStateChanged();
            try {
                EditText et = getEditText();
                Drawable editTextBackground = et.getBackground();
                editTextBackground.clearColorFilter();
    
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }