EditText setError() with icon but without Popup message

Problem solved after a lot of research and permutations- (Also thanks to @van)

Create a new class that will extend EditText something like this-

public class MyEditText extends EditText {

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void setError(CharSequence error, Drawable icon) {
    setCompoundDrawables(null, null, icon, null);
}
}

Use this class as a view in your xml like this-

<com.raj.poc.MyEditText
    android:id="@+id/et_test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

Now in the third step, just set a TextWatcher to your custom text view like this-

    et = (MyEditText) findViewById(R.id.et_test);

    errorIcon = getResources().getDrawable(R.drawable.ic_error);
    errorIcon.setBounds(new Rect(0, 0, errorIcon.getIntrinsicWidth(), errorIcon.getIntrinsicHeight()));
       et.setError(null,errorIcon);

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s.toString().length()>6){
                et.setError("", null);
            }else{
                et.setError("", errorIcon);
            }
        }
    });

where R.drawable.ic_error =

Keeping text null solves the problem
But if we keep only null in setError(null), this won’t show the validation error; it should be null along with second param.

Leave a Comment