When you try to use the EditText.setError()
method, the error message does not appear. The code below is used for performing the validation of 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;
}
}