Sunday, 18 June 2017

How to Create BMI Calculator Android App

How-to-Create-BMI-Calculator-Android-App


Download the Source Code for this BMI calculator

Downlod APK

I make this post simple as possible in the coding point of view.

You can read what is BMI and what is the formula to calculate our Body Mass Index.

I am simply explaining how to develop a BMI Calculator Android App.

Just read my previous posts like How to work with Buttons, How to add two numbers in Android and a Simple Calculator app.

If you have practiced the above examples it will be easy for you to do this BMI calculator app.

Create a new Project, drag and drop 2 Edittext to enter the height and weight, a button to calculate and a TextView to display the result also use the Hint.

BMI-calc-ui-design


After register the button, Edittext and Textview in MainActivity.java

register-ui-elements


After that write the below code inside the onClick()

@Override
    public void onClick(View arg0)
    {
       
       
        final DecimalFormat df2 = new DecimalFormat(".##");
        Double weight,height,heightinmeter,bmi,finalht;
        weight=Double.parseDouble(wt.getText().toString());
        height=Double.parseDouble(ht.getText().toString());
        heightinmeter=height*0.3048;
        finalht=heightinmeter*heightinmeter;
        bmi=weight/finalht;
        bmival.setText(""+df2.format(bmi));
       
        if(bmi<18.5)
         {
             res.setText("underweight");
         }
         else if(bmi>18.5&&bmi<24.9)
         {
             res.setText("normal weight");
         }
         else if(bmi>25&&bmi<29.9)
         {
             res.setText("overweight");
         }
         else if(bmi>30&&bmi<39.9)
         {
             res.setText("obesity");
         }
         else if(bmi>40)
         {
             res.setText("severe obesity");
         }
       
       
    }


BMI-calculator-source-code


  final DecimalFormat df2 = new DecimalFormat(".##");

this line is used to print only two digits after the dot for ex: 21.23 

heightinmeter=height*0.3048;
this line will convert your height in feet to height in meter

  Formula for calculating:

        heightinmeter=height*0.3048;// meter=feet*0.3048
        finalht=heightinmeter*heightinmeter;// height=meter*meter
        bmi=weight/finalht;
// bmi=weight/height

 This is the BMI standard

  if(bmi<18.5)
         {
             res.setText("underweight");
         }
         else if(bmi>18.5&&bmi<24.9)
         {
             res.setText("normal weight");
         }
         else if(bmi>25&&bmi<29.9)
         {
             res.setText("overweight");
         }
         else if(bmi>30&&bmi<39.9)
         {
             res.setText("obesity");
         }
         else if(bmi>40)
         {
             res.setText("severe obesity");
         }

The output will be

BMI-Calculator-Android-App

BMI-Calculator-Android-App

BMI-Calculator-Android-App
  
You can also add some colors to your app like this

BMI-Calculator-Android-App

BMI-Calculator-Android-App

I have used directly meter in this above example.