5

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?

m7majidi
  • 83
  • 2
  • 7
  • What do you want to happen instead? – Code-Apprentice Jun 24 '17 at 22:42
  • @Code-Apprentice i want to change only the error text, not the entire view – m7majidi Jun 24 '17 at 22:43
  • I am unclear about what I am looking at in your screenshot. Are these two different text fields in the same screen? Or are you trying to show a "before" and "after"? What is the text above and below the red text field? I see that the text above is the same as that in the white text field. Is this the data entered by the user? What about the red text below the red text field? Is this the error message? – Code-Apprentice Jun 24 '17 at 22:47
  • @Code-Apprentice these are a "before" and "after" screen shots. the text above is the hint and text below(the red one) is error text – m7majidi Jun 25 '17 at 07:40
  • this would be more clear if you post two separate pics. – Code-Apprentice Jun 25 '17 at 07:42
  • p.s. I do not have am answer for your question. I am trying to help you improve it so that I or anyone else can more easily help you. – Code-Apprentice Jun 25 '17 at 07:43

1 Answers1

1

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();
        }
    }
}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
m7majidi
  • 83
  • 2
  • 7
  • Looks like https://stackoverflow.com/a/40976082/2914140. So you can check `if (et != null) {` and `editTextBackground != null` and remove try-catch. – CoolMind Aug 31 '17 at 08:57