Check If Edittext Is Empty That Only Accepts Numbers
I have 2 Edittext which only accepts numbers. I want to make sure that the fields are not empty before the submit button is pressed to stop the application from crashing. These ar
Solution 1:
You can check that your EditTexts are not empty like below:
weightText.getText().length()!=0&&heightText.getText().length()!=0
And you can use this condition inside the onClick(View) method, which is called when your button is clicked like below:
yourButton.setOnClickListener( new OnClickListener(){
publicvoidonClick(){
if(weightText.getText().length() != 0 && heightText.getText().length() != 0){
//do sth
}
});
The second way, you can create TextWatcher which set to both EditTexts and in onTextChanged() you can check that both EditTexts are not empty like below:
privateclassYourTextWatcherimplementsTextWatcher{
@OverridepublicvoidbeforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@OverridepublicvoidonTextChanged(CharSequence charSequence, int i, int i2, int i3) {
yourButton.setEnabled(weightText.getText().length() != 0 && heightText.getText().length() != 0)
}
@OverridepublicvoidafterTextChanged(Editable editable) {
}
}
you can set this TextWatcher for your EditText like below:
weightText.addTextChangedListener(newYourTextWatcher());
heightText.addTextChangedListener(newYourTextWatcher());
Here is code that your Activity should look like:
publicclassBMIextendsActivity {
EditText weightText;
EditText heightText;
TextView resultText;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
weightText = (EditText) findViewById(R.id.WeightText);
heightText = (EditText) findViewById(R.id.Heighttext);
resultText = (TextView) findViewById(R.id.resultLabel);
Buttonbutton= (Button) findViewById(R.id.calulate);
button.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
calculateHandler();
}
});
}
publicvoidcalculateHandler() {
// make sure we handle the click of the calculator buttonif (weightText.getText().length() == 0 || heightText.getText().length() == 0) {
return;
}
// get the users values from the widget referencesfloatweight= Float.parseFloat(weightText.getText().toString());
floatheight= Float.parseFloat(heightText.getText().toString());
// calculate the bmi valuefloatbmiValue= calculateBMI(weight, height);
// interpret the meaning of the bmi valueStringbmiInterpretation= interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + " = " + bmiInterpretation);
// display toast additionally to example
Toast.makeText(this, bmiValue + " = " + bmiInterpretation, Toast.LENGTH_LONG).show();
}
privatefloatcalculateBMI(float weight, float height) {
return (float) Math.round((weight / (height * height)) * 100) / 100;
}
// interpret what BMI meansprivate String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return"Severely underweight";
} elseif (bmiValue < 18.5) {
return"Underweight";
} elseif (bmiValue < 25) {
return"Normal";
} elseif (bmiValue < 30) {
return"Overweight";
} else {
return"Obese";
}
}
}
Calculate method
publicvoidcalculateHandler() {
// make sure we handle the click of the calculator buttonif (weightText.getText().toString().trim().length() == 0 || heightText.getText().toString().trim().length() == 0) {
Toast.makeText(this, "Please fill all field by numbers", Toast.LENGTH_LONG).show();
return;
}
// get the users values from the widget referencesfloat weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi valuefloat bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + " = " + bmiInterpretation);
// display toast additionally to example
}
Solution 2:
weightText.getText().toString().isEmpty();
Solution 3:
try like this..
String weight = weightText.getText().toString();
String height = heightText.getText().toString();
if((Integer.parseInt(weight)!=null) && (Integer.parseInt(height)!=null)){
//Not an empty
}
else{
//empty
}
Post a Comment for "Check If Edittext Is Empty That Only Accepts Numbers"