I'm developing a mobile application in android studio and I'm having a hard time implementing validation of the form's input fields.
Can anyone explain why I can not validate?
Form Class:
public class FormPalhetasActivity extends Activity implements
AdapterView.OnItemSelectedListener, LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_form_palhetas );
frua = (EditText) findViewById(R.id.rua);
fbairro = (EditText) findViewById(R.id.bairro);
fcompl = (EditText) findViewById(R.id.compl);
fcidade = (EditText) findViewById(R.id.cidade);
festado = (EditText) findViewById(R.id.estado);
fpais = (EditText) findViewById(R.id.pais);
fobs = (EditText) findViewById(R.id.observacao);
fcod = (EditText) findViewById(R.id.cod);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Terreno baldio");
categories.add("Esgoto a ceu aberto");
categories.add("Lixo");
categories.add("Piscina");
categories.add("Construção");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
readMyCurrentCoordinates();
Button enviar = (Button) findViewById( R.id.enviar );
enviar.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if(valid() == true){
registerForms();
}
}
} );
}
public static boolean validateNotNull(View pView, String pMessage) {
if (pView instanceof EditText) {
EditText edText = (EditText) pView;
Editable text = edText.getText();
if (text != null) {
String strText = text.toString();
if (!TextUtils.isEmpty(strText)) {
return true;
}
}
// em qualquer outra condição é gerado um erro
edText.setError(pMessage);
edText.setFocusable(true);
edText.requestFocus();
return false;
}
return false;
}
public boolean valid(){
if(validateNotNull(frua,"Preencha o campo rua")) {
return false;
}if(validateNotNull(fcompl,"Preencha o campo complemento")) {
return false;
}if(validateNotNull(fbairro,"Preencha o campo bairro")) {
return false;
}if(validateNotNull(fcidade,"Preencha o campo cidade")) {
return false;
}if(validateNotNull(festado,"Preencha o campo estado")) {
return false;
}if(validateNotNull(fobs,"Preencha o campo observação")) {
return false;
}if(validateNotNull(fpais,"Preencha o campo país")) {
return false;
}if(validateNotNull(fpais,"Preencha o campo país")) {
return false;
}else{
return true;
}
}
Output: No notification appears that the field is blank.