How to Develop Body Mass Index(BMI) Android App

Body mass index app is an app that calculates the fitness of our body. It takes two paraments which are the weight and height of the person then processes the calculation and it judge your fitness status.

There are different fitness statuses as BMI indicators

  1. Several Underweight
  2. Under Weight
  3. Normal
  4. Over Weight
  5. Obesity

How App Work

Apps can work basic calculations based on the parameters you feed. In general, we inter two paraments weight and height then it is calculated by using the body mass index formula

bmi=weight/(height * height)/100

Let’s start Coding with using Android studio

Open Android studio setups and select Basic View Activity

Enter your project name BMI

Choose Java for development Technology

Java is a very powerful Android application development, next to Kotlin.

Select the Android version that you want to develop

Click finish at the bottom of the setups

How to work with Android code

After creating an Android application there are two-step process to get applications with codes.

Any android Code has two parts XML and Java part.

XML Part

XML is the interface design of Android applications by using XML we can create beautifully designed and colorful layouts. In most apps creating design it takes more than working on Java applications.

How we design the BMI app by using XML

Let BMI have two user input fields, three text view fields, and two buttons

copy these codes and past it activty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#009688"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:fontFamily="@font/alfa_slab_one"
        android:rotation="-9"
        android:rotationX="-13"
        android:rotationY="17"
        android:text="BMI"
        android:textColor="#C61212"
        android:textColorLink="#E61515"
        android:textSize="60sp"
        app:layout_constraintBottom_toTopOf="@+id/edh"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.451"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.321" />

    <EditText
        android:id="@+id/edw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="52dp"
        android:ems="10"
        android:hint="Weight"
        android:inputType="text"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/edh"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.548"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="1.0" />

    <EditText
        android:id="@+id/edh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:ems="10"
        android:hint="Height"
        android:inputType="text"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/calculate"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.624"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edw" />

    <Button
        android:id="@+id/calculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Calculate"
        android:textColorLink="#2B78B6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.174"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edh" />

    <TextView
        android:id="@+id/inter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:text=" "
        android:textColor="#DD3131"
        android:textSize="34sp"
        android:textStyle="bold|italic"
        app:layout_constraintBottom_toTopOf="@+id/result"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/calculate" />

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="20sp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:rotationY="6"
        android:text=" "
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.192"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inter" />

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Reset"
        android:textColorLink="#DF1C1C"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.629"
        app:layout_constraintStart_toEndOf="@+id/calculate"
        app:layout_constraintTop_toBottomOf="@+id/edh" />
</androidx.constraintlayout.widget.ConstraintLayout>

The above code is used to design user interface

MainActivity.java

MainActivity has the main function of the application. It is the back-end processing of the applications. All logical operations are performed in the MainActivity.java file.

Copy the following code and past it to your MainActivity.Java

package com.example.bmi;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        EditText edh,edw;
        Button calculate,resetbtn;
        TextView result,textinter;
        edh=findViewById(R.id.edh);
        edw=findViewById(R.id.edw);
        calculate =findViewById(R.id.calculate);
        resetbtn =findViewById(R.id.reset);
        result= findViewById(R.id.result);
        textinter= findViewById(R.id.inter);
    calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String strwe=edw.getText().toString();
            String strhe=edh.getText().toString();
            if(strhe.equals("")){
                edh.setError("Please Enter Height!");
                edh.findFocus();
                return;
            }
            if(strwe.equals("")){
                edw.setError("Please Enter Weight!!");
                edw.findFocus();
                return;
            }
            float weight = Float.parseFloat(strwe);
            float height = Float.parseFloat(strhe)/100;

            float bmiValue=BMICalcualte(weight,height);

            textinter.setText(InterprateBMI(bmiValue));
            result.setText("BMI = "+ bmiValue);





        }
    });
        resetbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edh.setText("");
                edw.setText("");
                textinter.setText("");
                result.setText("");
            }
        });

    }
    public Float BMICalcualte(Float weight, Float height){
        return weight / (height * height);
    }
    public String InterprateBMI(Float bmiValue){
        if(bmiValue < 16){
            return "Severerl Under Weight";
        }
        else if(bmiValue <= 18.5){
            return  "Under weight";
        }
        else if(bmiValue < 25){
            return "Normal";
        }
        else if(bmiValue < 30){
            return "Over Weight";
        }
        else{
            return "Obses please work hard!!";
        }
    }

}

The above code is used to perform mathematical and logical operations in the background. To run the application MainActivity.Java has total roles.

If you have an error on some line of code you can not run the project and you must fix it and re-run the project.

To Download the project you must follow the following steps.

  1. Click download button
  2. Extract file
  3. Open your Android Studio and import
  4. To import Select File-> New -> Import Project
  5. Choose the location your project found and import it.

You can also download source code from these locations.

https://bbn11.com/wp-content/uploads/2024/04/BMI.zip

Download apk from these links.

https://bbn11.com/wp-content/uploads/2024/04/bmi.zip

Summary

Android application is an application that has apk files.

To develop an Android application you must follow the following steps.

Remember Android Studio takes very large memory usage from your PC.

To install Android Studio we must consider the following requirements.

👉Download Android Studio

👉 Install Android studio

👉 Download SDK and install with Android studio

👉 Open Android Studio and click new Project

👉 Select the activity that you want, Empy Activity, Basic View Activity, and others

👉 Click next and give your project name, select development technology, java or kotlin

👉 Click the Finish button and get your project,

Remember at this time your machine must be connected to the internet, It downloads some file from the internet and sinks to our projects. Based on your connection download time may vary from one PC to another PC.

Remember: Telegram Channel  https://t.me/tobcoders

Youtube Channels https://www.youtube.com/@borufantube

Author: bbnadmin

1 thought on “How to Develop Body Mass Index(BMI) Android App

Leave a Reply

Your email address will not be published. Required fields are marked *