Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 635
m7majidi Avatar asked Nov 08 '22 20:11

m7majidi


1 Answers

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();
        }
    }
}
like image 79
m7majidi Avatar answered Nov 27 '22 11:11

m7majidi