Nick
Nick

Reputation: 9373

EditText setError() with no message just the icon

Currently I have a really simple code that validates EditText fields and when the user hits the button at the end it checks them. It will put errors in all the fields with this:

if (!emailmatcher.matches()) {
    email.setError("Invalid Email");  
}
if (!zipmatcher.matches()) {
    zipcode.setError("Invalid ZipCode");                     
}

My problem that is the keyboard will popup and will move the error bubble to a random place. I was hoping to not have a error bubbles with a message but keep the error icons in invalid EditText fields. I tried inputting setError(null) but that doesn't work. Any Ideas?

Upvotes: 41

Views: 23805

Answers (5)

BK19
BK19

Reputation: 1534

This works for me. Now getting error messages pop up open.

public boolean isValid() {
        String email = ediTextEmail.getText().toString();
        String password = ediTextPassword.getText().toString();
        if (TextUtils.isEmpty(email)) {
            ediTextEmail.requestFocus();
            ediTextEmail.setError("Email cannot be blank");
            return false;
        }

        if (!isEmailValid(email)) {
            ediTextEmail.requestFocus();
            ediTextEmail.setError("Enter a valid email id");
            return false;
        }

        if (TextUtils.isEmpty(password)) {
            ediTextPassword.requestFocus();
            ediTextPassword.setError("Password cannot be blank");
            return false;
        }

        mEmail = email;
        mPassword = password;
        return true;
    }

Pop opened even if my focus is on password editText, it will be redirected to email editText.

Upvotes: 3

Alex Zaitsev
Alex Zaitsev

Reputation: 1781

You can use my custom solution for popups. In this case popup is independent from EditText focus state. Moreover it is usable with multiple EditTexts. https://plus.google.com/+AlexanderZaitsevDev/posts/W9XcZNZrsMB

Upvotes: 0

Tofeeq Ahmad
Tofeeq Ahmad

Reputation: 11975

Your code perfect to show icon only.

As EditText is show anything related to when it has focused. So just change your code to like that...

if (!emailmatcher.matches()) {
    email.requestFocus();
    email.setError("Invalid Email");  
}
if (!zipmatcher.matches()) {
    zipcode.requestFocus();
    zipcode.setError("Invalid ZipCode");                     
}

Upvotes: 63

Amol Desai
Amol Desai

Reputation: 927

simply create your own edittext and override setError

    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);
 }
}  

and then use wherever you want et.setError("", drawableToShow);

Upvotes: 0

You can use

setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.my_error_icon, 0);

to set the error icon to the right of your text input. You may also have to register

View.OnKeyListener to reset the error icon on user input:

EditText myEdit = (EditText)findViewById(R.id.my_edit);

if (TextUtils.isEmpty(myEdit.getText().toString())) {
    // set the warning
    myEdit.requestFocus();

    // show error icon
    myEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_sign, 0);

    // remove the error icon on key press
    myEdit.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            EditText eText = (EditText)v;

            // remove error sign
            eText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

            // remove this key handler
            eText.setOnKeyListener(null);

            return false;
        }
    });
}

Upvotes: 5

Related Questions