Peter Pei Guo
Peter Pei Guo

Reputation: 7870

Android setError(): error message visually pointing to the wrong field

I use setError() to show validation errors. In one case, the field is close to the left side of the screen, when the error message showed up, it visually pointed to the wrong EditText field - instead of pointing to the field showing 1234, it should really point to the blank field on the left, see attached screenshot. Does anyone know a solution to this? Thanks!

The screenshot was taken on a Galaxy 3 phone running Android 4.4.2.

enter image description here

Upvotes: 3

Views: 865

Answers (2)

Peter Pei Guo
Peter Pei Guo

Reputation: 7870

I got one workaround, add \n to format the message and reduce the width of each line:

text1.setError("this field must\nbe 1 character\nin length");

Upvotes: 1

Darish
Darish

Reputation: 11501

This is the solution for your problem.. Try this..

EditText edt1=(EditText) findViewById(R.id.editText2);
    EditText edt2=(EditText) findViewById(R.id.editText3);
    EditText edt3=(EditText) findViewById(R.id.editText1);
    // Reset errors.
    edt1.setError(null);
    edt2.setError(null);
    edt3.setError(null);

    String  txt3 = edt3.getText().toString().trim();
    String  txt1 = edt1.getText().toString().trim();
    String txt2=edt2.getText().toString().trim();
    boolean cancel = false;
    View focusView = null;
    if (TextUtils.isEmpty(txt3)) {
        edt3.setError(getString(R.string.error_field_required));
        focusView = edt3;
        cancel = true;
    }
    if (TextUtils.isEmpty(txt2)) {
        edt2.setError(getString(R.string.error_field_required));
        focusView = edt2;
        cancel = true;
    }

    if (TextUtils.isEmpty(txt1)) {
                edt1.setError(getString(R.string.error_field_required));
                focusView = edt1;
                cancel = true;
            }



            if (cancel) {

                focusView.requestFocus();
            } else {

                               //do the operations you want do here

            }

Upvotes: 0

Related Questions