Drawable right in edit text not updating after error
Asked Answered
V

3

10

Edit text in android doesn't allow to change draw able after setError. I have used drawable right for password field but if error comes in password field it won't allow to change draw able after it. before error it works fine.

<EditText
                android:id="@+id/edt_reg_password"
                style="@style/editText_full_view"
                android:layout_height="wrap_content"
                android:layout_below="@id/edt_reg_email"
                android:layout_marginTop="@dimen/padding_normal"
                android:drawableLeft="@mipmap/ic_action_password"
                android:drawableRight="@mipmap/ic_action_password_visibility"
                android:drawablePadding="@dimen/padding_normal"
                android:hint="@string/hint_password"
                android:inputType="textPassword"
                android:maxLength="25"
                android:paddingLeft="@dimen/padding_normal"
                tools:visibility="visible" />

Java code for changing eye icon run time

private void setPasswordDrawable()
    {
        final Drawable showpass_icon = getResources().getDrawable(R.mipmap.ic_action_password_visibility);
        final Drawable hidepass_icon = getResources().getDrawable(R.mipmap.ic_action_password_visibility_off);


        final Drawable pass_drawable = getResources().getDrawable(R.mipmap.ic_action_password);
        pass_drawable.setBounds(0, 0, pass_drawable.getIntrinsicWidth(), pass_drawable.getIntrinsicHeight());


        //edtPassword.setCompoundDrawables(pass_drawable, null, showpass_icon, null);

        edtPassword.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (edtPassword.getCompoundDrawables()[2] == null) {
                    return false;
                }
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (event.getX() > edtPassword.getWidth() - edtPassword.getPaddingRight()
                        - showpass_icon.getIntrinsicWidth()) {

                    if (isPasswordVisible) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //edtPassword.setError(null);
                                edtPassword.setTransformationMethod(
                                        PasswordTransformationMethod.getInstance());
                                edtPassword.setSelection(edtPassword.getText().length());

                                showpass_icon.setBounds(0, 0, showpass_icon.getIntrinsicWidth(), showpass_icon.getIntrinsicHeight());
                                edtPassword.setCompoundDrawables(pass_drawable, null, showpass_icon, null);


                            }
                        });

                        isPasswordVisible = false;
                    } else {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //edtPassword.setError(null);
                                edtPassword.setTransformationMethod(
                                        HideReturnsTransformationMethod.getInstance());
                                edtPassword.setSelection(edtPassword.getText().length());
                                hidepass_icon.setBounds(0, 0, hidepass_icon.getIntrinsicWidth(), hidepass_icon.getIntrinsicHeight());
                                edtPassword.setCompoundDrawables(pass_drawable, null, hidepass_icon, null);
                            }
                        });

                        isPasswordVisible = true;
                    }
                }
                return false;
            }
        });

    }

For setting error

public void setViewError(View view, String message)
{
    if (view instanceof EditText) {
         ((EditText) view).setError(message);
   }
}

live example

Vivienne answered 11/1, 2016 at 8:48 Comment(2)
Did you get the answer?Hilton
@PriyankaAlachiya not tried with latest support library. done with some other way.Vivienne
C
0

You may use this like -

if(error=true){
    editText.setCompoundDrawablesWithIntrinsicBounds(
        0, 0,R.drawable.ic_error, 0);
    editText.setCompoundDrawablePadding(5);}
else{
    editText.setCompoundDrawablesWithIntrinsicBounds(
        0, 0,R.drawable.ic_corrct, 0);
    editText.setCompoundDrawablePadding(5);}
Circlet answered 11/1, 2016 at 8:59 Comment(3)
its not working, i have tried this also. i think once android set error drwable it is not allowing other drawable to replace it. i have even tried to setError(null) also.Vivienne
What is your minimum and target sdk?Circlet
minSdkVersion 15 , targetSdkVersion 23 y ? does this matters ?Vivienne
V
0

It may be very late. Me too faced these kind of issue. And I frustrated to find the solution. Finally I found the solution. It may helpful someone like me. The solution is very simple.

EditText stores the errors whenever we set the error and it didn't remove automatically even after entering the correct value. So you need to manually override the error text and set empty value.

    if (!isError) {
        edt_reg_password.setError("");
        edt_reg_password.setText(edt_reg_password.getText().toString());
    } else {
        edt_reg_password.setError("Data Invalid");
    }

Thanks ! Happy Coding !

Voronezh answered 6/2, 2020 at 11:23 Comment(0)
L
0

The right drawable and error drawable of the TextView (or EditText) are displayed based on the same field.

When the error drawable is showing, the right drawable will be stored in a temporary variable and then restored when the error disappears.

So you can't change the right drawable when the error is showing.

But there is a problem with the code in TextView: the temporary variable (right drawable) will not be clear after restoration.

And once the temporary variable exists, even if you set the right drawable with a new value, the temporary variable will be display anyway.

This is why right drawable not change after setError(): it keep showing the previous value.

All this happens in TextView#Drawables.

Unfortunately there is no direct way to that clear temporary variables, but this can be done indirectly by set TextView#mDrawables to null. Then, you can set the right drawable as you want.

// Clear error first
editText.setError(null);
// Clear TextView#Drawables
editText.setCompoundDrawables(null, null, null, null);
// Set right drawable
editText.setCompoundDrawables(null, null, drawableRight, null);
Leverick answered 20/9, 2022 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.