Problems using the Edittext.setError() method

Asked

Viewed 971 times

3

When trying to use the method EditText.setError() the error message does not appear. The code below is used to validate the required fields:

public class LoginActivity extends Activity {

    private EditText usuario;
    private EditText senha;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        usuario = (EditText) findViewById(R.id.usuario);
        senha = (EditText) findViewById(R.id.senha);

    }

    private boolean loginValido() {

        EditText campoComFoco = null;

        boolean isValid = true;

        if (usuario.getText().toString().length() == 0) {
            campoComFoco = usuario;
            usuario.setError("Usuário obrigatório");
            isValid = false;
        }
        if (senha.getText().toString().length() == 0) {
            if (campoComFoco == null) {
                campoComFoco = senha;
            }
            senha.setError("Senha obrigatória");
            isValid = false;
        }

        if (campoComFoco != null) {
            campoComFoco.requestFocus();
        }

        return isValid;
    }

}

inserir a descrição da imagem aqui

  • 1

    Dude, without the code you made, it gets a little tricky telling you what’s missing.. Edit your question and add the relevant part of the code.

  • A question. Are you trying to put the error in a TextView or in a EditText?

  • Sorry, @sicachester, Edittext.

2 answers

1

I changed my answer, according to your edition

 final EditText txtNome = (EditText) findViewById(R.id.txtHint);

        txtNome.setOnEditorActionListener(new EditText.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
                {
                    String campoPesquisa = txtNome.getText().toString();

                    if (TextUtils.isEmpty(campoPesquisa))
                    {
                        editText.setError("Informe o usuario");
                        vibrar(200);
                        return false;
                    }
                    }
                }

                return false; 
            }
        });

I recommend using the TextUtils then if you want to validate whether the field is empty.

onEditorAction: allows to define what will be the end action of the user, in the example that I removed from an application of mine it triggers when the user click on search and when click on Ok or done.

  • @Geisonsantos, puts where you are declaring them, is an editText even right? Another question he is pointing error? only not present message?

  • @Geisonsantos, I changed my answer

0

Your code is correct, you just need to add one "phocus" to the EditText who is trying to assign the setError().

Try something similar:

if (usuario.getText().toString().length() == 0) {
    campoComFoco = usuario;
    //Adicionando o foco no seu EditText
    usuario.requestFocus();
    usuario.setError("Usuário obrigatório");
    isValid = false;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.