Android, Validate An Empty Edittext Field
Hi I have a simple temperature converter application where the application crashes whenever the field is empty. I tried some validation I found on S.O but it keeps crashing, so can
Solution 1:
Try this
if (celsiusEdit.getText().toString().matches("")) {
Toast.makeText(this, "Field cannot be empty", Toast.LENGTH_SHORT).show();
return;
}else{
double celsius = Double.valueOf(celsiusEdit.getText().toString());
double farh = (celsius -32) * 5/9;
farhEdit.setText(String.valueOf(farh));
}
Solution 2:
Your if statement makes no sense, you should do something like this:
if(celsiusEdit.getText().toString().trim().length() == 0){
However you could run into an issue if a non-number is put into that textbox because
Double celsius = Double.valueOf(celsiusEdit.getText().toString());
will throw a NumberFormatException, so you should put that in a try.
Solution 3:
You can try and import this class here Android Form Validation Class
This can help you to validate the Form fields, just need to follow the instructions. (:
Post a Comment for "Android, Validate An Empty Edittext Field"