0

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 are the 2 edittext fields i have.

EditText weightText = (EditText)findViewById(R.id.WeightText);
EditText heightText = (EditText)findViewById(R.id.Heighttext);

Code:

public class BMI extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.bmi);
        }


        public void calculateHandler(View view) {
            // make sure we handle the click of the calculator button

            if (view.getId() == R.id.calculate) {

                // get the references to the widgets
                EditText weightText = (EditText)findViewById(R.id.WeightText);
                EditText heightText = (EditText)findViewById(R.id.Heighttext);
                TextView resultText = (TextView)findViewById(R.id.resultLabel);



                // get the users values from the widget references

                float weight = Float.parseFloat(weightText.getText().toString());
                float height = Float.parseFloat(heightText.getText().toString());

                // calculate the bmi value

                float 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);
            }
        }

        private float calculateBMI (float weight, float height) {


            return (float)Math.round((weight / (height * height)) * 100) / 100 ;
        }


        // interpret what BMI means
        private String interpretBMI(float bmiValue) {

            if (bmiValue < 16) {
                return "Severely underweight";
            } else if (bmiValue < 18.5) {

                return "Underweight";
            } else if (bmiValue < 25) {

                return "Normal";
            } else if (bmiValue < 30) {

                return "Overweight";
            } else {
                return "Obese";
            }

        }

    }
user3411002
  • 783
  • 3
  • 9
  • 27

3 Answers3

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(){
   public void onClick(){
       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:

private class YourTextWatcher implements TextWatcher{
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        yourButton.setEnabled(weightText.getText().length() != 0 && heightText.getText().length() != 0)
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}

you can set this TextWatcher for your EditText like below:

     weightText.addTextChangedListener(new YourTextWatcher());
     heightText.addTextChangedListener(new YourTextWatcher());

Here is code that your Activity should look like:

    public class BMI extends Activity {

        EditText weightText;
        EditText heightText;
        TextView resultText;

        @Override
        public void onCreate(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);


            Button button = (Button) findViewById(R.id.calulate);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    calculateHandler();
                }
            });
        }


        public void calculateHandler() {
            // make sure we handle the click of the calculator button
            if (weightText.getText().length() == 0 || heightText.getText().length() == 0) {
                return;
            }
            // get the users values from the widget references

            float weight = Float.parseFloat(weightText.getText().toString());
            float height = Float.parseFloat(heightText.getText().toString());

            // calculate the bmi value

            float 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
            Toast.makeText(this, bmiValue + " = " + bmiInterpretation, Toast.LENGTH_LONG).show();
        }

        private float calculateBMI(float weight, float height) {
            return (float) Math.round((weight / (height * height)) * 100) / 100;
        }


        // interpret what BMI means
        private String interpretBMI(float bmiValue) {
            if (bmiValue < 16) {
                return "Severely underweight";
            } else if (bmiValue < 18.5) {

                return "Underweight";
            } else if (bmiValue < 25) {

                return "Normal";
            } else if (bmiValue < 30) {

                return "Overweight";
            } else {
                return "Obese";
            }
        }
    }

Calculate method

public void calculateHandler() {
    // make sure we handle the click of the calculator button
    if (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 references

    float weight = Float.parseFloat(weightText.getText().toString());
    float height = Float.parseFloat(heightText.getText().toString());

    // calculate the bmi value

    float 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

}
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
1
weightText.getText().toString().isEmpty();
Simar
  • 600
  • 4
  • 17
0

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
}
M S Gadag
  • 2,057
  • 1
  • 12
  • 15