Friday, 2 September 2016

Mandatory fields in android



Hello I am making an app where user can order some books. When he, or she, fill out the order with his firs second name etc and hits order button applications,using Intent, will switch to SMS and thus will purchase books via text message.But i wish to be able, if user accidently forget to fill up all fields, toast pop up with message "Please fill up XYZ field". I used if else, but when some field remain empty something else happened.I got android message that my app needs to be closed and and return me to the previous activity.In my LogCat nothing happened . No error message.



this is my code :



public void kreiranjeNarudzbine(View v) {

EditText editTextIme = (EditText) findViewById(R.id.ime);
String imeNarucioca = editTextIme.getText().toString();
EditText editTextPrezime = (EditText)findViewById(R.id.prezime);
String prezimeNarucioca = editTextPrezime.getText().toString();
EditText editTelefonNarucioca = (EditText)findViewById(R.id.telefon);
String telefonNarucioca = editTelefonNarucioca.getText().toString();
EditText editAdresaNarucioca = (EditText)findViewById(R.id.adresa);
String adresaNarucioca = editAdresaNarucioca.getText().toString();
EditText editGradNarucioca = (EditText)findViewById(R.id.grad);
String gradNarucioca = editGradNarucioca.getText().toString();

EditText editKolicina = (EditText)findViewById(R.id.kolicina);
String narucenaKolicina = editKolicina.getText().toString();
int kolicina = Integer.parseInt(narucenaKolicina);
int cenaNarudzbine = cena(kolicina);
String poruka = sumiranjeNarudzbine(imeNarucioca, prezimeNarucioca,telefonNarucioca,adresaNarucioca,gradNarucioca,cenaNarudzbine);
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "+381629647169");
smsIntent.putExtra("sms_body",poruka);
if(imeNarucioca.equals("")){

Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
}
else if(prezimeNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();

}
else if(telefonNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
}
else if(adresaNarucioca.equals("")){

Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
}
else if(gradNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
}
else if(narucenaKolicina.equals("")){
Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
}
else{


startActivity(smsIntent);}

}

Answer



As already mentioned, you check to make sure it isn't null or empty.



if(imeNarucioca!=null && imeNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
}

else if(prezimeNarucioca!=null && prezimeNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();

}
else if(telefonNarucioca!=null && telefonNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
}
else if(adresaNarucioca!=null && adresaNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
}

else if(gradNarucioca!=null && gradNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
}
else if(narucenaKolicina!=null && narucenaKolicina.equals("")){
Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
}
else{
startActivity(smsIntent);
}



This can be converted into a method to prevent as much text:



public boolean isETEmpty(EditText et){
return (et != null && (et.equals("") || et.equals(" ")));//the final piece checks if the edittext is empty or just contains a space. It is or between
//in order to ensure that one of those are true. Thus the parenthesis
}


(how the above works)




Now, for the required fields you can use HTML to format the text:



/*your view*/.setText(Html.fromHtml("Your title. *"));


The above code formats the code by adding a red star after the required fields. Later down you can do this:



/*your view*/.setText(Html.fromHtml("* Required field"));



The comment /*your view*/ you replace with a textview reference, edittext or whatever you use to set text






Further reading:



https://stackoverflow.com/a/9161958/6296561


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...