Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Build a Body Mass Index Calculator in Android Studio?

  • Last Updated : 03 Sep, 2021

The Body Mass Index (BMI) Calculator can be used to calculate BMI values based on height and weight. BMI is a fairly reliable indicator of body fatness for most people.

Formula:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

BMI = (weight) / (height * height)

Approach: BMI is a number calculated from an individual’s weight and height. To find out BMI we will take input from the user (both height and weight) which will be stored in height and weight variables for further calculation. The calculation process is simple, we will simply divide weight in kilograms by the square of the height. Now as per the BMI calculated, it will execute the respective if-else statement. We are also checking if the user is pressing submit button without entering the inputs, in that case, we will display a Toast message saying “Please enter the valid height and weight”.  In this article, we will be building Body Mass Index Calculator in Android Studio using Kotlin and XML. 



Step by Step Implementation

Step 1: Create a New Project 

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the build.gradle(Module) File

You need to apply the plugin kotlin-android-extensions in app build.gradle module like this

plugins {

   id ‘com.android.application’

   id ‘kotlin-android’

   id ‘kotlin-android-extensions’



}

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="Body Mass Index Calculator"
        android:textSize="25sp"
        android:textStyle="bold" />
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="Weight(in Kg)"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.292" />
  
    <EditText
        android:id="@+id/etWeight"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp" />
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="Height (in cm)"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.292" />
  
    <EditText
        android:id="@+id/etHeight"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:textSize="20dp" />
  
    <Button
        android:id="@+id/calculate_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40dp"
        android:backgroundTint="#2CD3BE"
        android:text="Calculate" />
  
    <TextView
        android:id="@+id/bmi_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="The BMI is "
        android:textSize="20dp"
        android:visibility="gone" />
  
    <TextView
        android:id="@+id/bmi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="BMI"
        android:textSize="20dp"
        android:visibility="gone" />
  
    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="Status"
        android:textSize="20dp"
        android:textStyle="bold"
        android:visibility="gone" />
  
    <Button
        android:id="@+id/ReCalculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:backgroundTint="#EAA268"
        android:text="Recalculate"
        android:visibility="gone" />
      
</androidx.appcompat.widget.LinearLayoutCompat>

After writing this much code our UI looks like this:

Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. 

Kotlin




import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        calculate_btn.setOnClickListener {
  
            // Check if the height EditText and Weight EditText are not empty
            if (etHeight.text.isNotEmpty() && etWeight.text.isNotEmpty()) {
                val height = (etHeight.text.toString()).toInt()
                val weight = (etWeight.text.toString()).toInt()
  
                // calculateBMI will return BMI
                val BMI = calculateBMI(height, weight)
  
                bmi.text = BMI.toString()
                bmi.visibility = View.VISIBLE
  
                // update the status text as per the bmi conditions
                if (BMI < 18.5) {
                    status.text = "Under Weight"
                } else if (BMI >= 18.5 && BMI < 24.9) {
                    status.text = "Healthy"
                } else if (BMI >= 24.9 && BMI < 30) {
                    status.text = "Overweight"
                } else if (BMI >= 30) {
                    status.text = "Suffering from Obesity"
                }
  
                bmi_tv.visibility = View.VISIBLE
                status.visibility = View.VISIBLE
  
                ReCalculate.visibility = View.VISIBLE
                calculate_btn.visibility = View.GONE
                  
            }
  
            // when either Weight EditText or 
            // height EditText have null value
            // we will display toast.
            else {
                Toast.makeText(this, "please enter the valid height and weight", Toast.LENGTH_SHORT).show()
            }
        }
          
        ReCalculate.setOnClickListener {
            ResetEverything()
        }
  
    }
  
    // Function to reset all Text and EditText fields.
    private fun ResetEverything() {
          
        calculate_btn.visibility = View.VISIBLE
        ReCalculate.visibility = View.GONE
  
        etHeight.text.clear()
        etWeight.text.clear()
        status.text = " "
        bmi.text = " "
        bmi_tv.visibility = View.GONE
    }
  
    // Function for calculating BMI
    private fun calculateBMI(height: Int, weight: Int): Float {
          
        val Height_in_metre = height.toFloat() / 100
        val BMI = weight.toFloat() / (Height_in_metre * Height_in_metre)
  
        return BMI
    }
}

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :