BMI CALCULATOR IN ANDROID

top banner

BMI CALCULATOR IN ANDROID

BMI stands for Body Mass Index, which is a measure of a person’s body fat based on their weight and height. It is a commonly used tool to assess whether a person’s weight is healthy or not.

The calculation of BMI involves dividing a person’s weight in kilograms by the square of their height in meters. The result of this calculation is then used to categorize the person’s weight status based on a range of BMI values:

  • Below 18.5: underweight
  • 18.5-24.9: normal weight
  • 25-29.9: overweight
  • 30 or above: obese

However, it’s important to note that BMI is not a perfect measure of body fat, as it doesn’t take into account factors such as muscle mass, bone density, and overall body composition. Therefore, it should be used as a general guide rather than a definitive assessment of a person’s health. It’s always best to consult with a healthcare professional for an accurate assessment of your weight and overall health.

CREATE A NEW ANDROID PROJECT.

1. Open Android Studio and click on “Start a new Android Studio project” from the welcome screen. If you already have a project open, you can create a new one by going to “File” > “New” > “New Project”.
firstproject1

2. In the “Create New Project” dialog, choose “Empty Activity” from the list of templates and click on “Next”.
3. Enter the name of your app in the “Application name” field. This will be the name that appears on the user’s device when they install your app. You can also choose the package name, which is a unique identifier for your app.
4. Choose the minimum API level that your app will support. This determines the oldest version of Android that your app can run on. You can also choose the language that you want to use for your app development. Once you have made your selections, click on “Finish”.
firstproject2

Android Studio will now create a new project with an empty activity. The activity is a basic screen that users will see when they open your app. You can customize this activity by adding UI elements and functionality.

REMOVE THE ACTION BAR IN MAIN ACTIVITY.

First of all I wish to remove the action bar in the main activity of an Android app:
1. Open the main activity java file in Android Studio.
2. In the onCreate() method of the activity, add the following code snippet:

getSupportActionBar().hide(); 

3. This will hide the action bar from the main activity. However, if you want to remove the action bar from the entire app, you can add the same line of code to the onCreate() method of every activity in your app.
4. If you want to remove the action bar permanently from your app, you can do this by modifying the app theme in your styles.xml file. To do this, open the styles.xml file located in the res/values folder of your project.
5. In the styles.xml file, add the following line of code to the AppTheme style:

<item name="windowActionBar">false</item> 

This will remove the action bar from all activities in your app that use the AppTheme style.

LAYOUT RESOURCE FOR BMI CALCULATOR.

You can set up the activity layout resource and Java code implementation for a BMI calculator with a standard look. Here’s an example:
1. Now open the activity_main.xml file located in the “res/layout” folder.
2. In the activity_main.xml file, add the following code to set up the layout for the BMI calculator:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="16dp"> 
<TextView 
android:text="BMI Calculator"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textSize="24sp"
 android:textStyle="bold"
 android:gravity="center"/> 
<EditText
android:id="@+id/editTextWeight" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your weight in kg"
android:inputType="numberDecimal" 
android:layout_marginTop="16dp"/> 
<EditText
android:id="@+id/editTextHeight" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your height in cm" 
android:inputType="numberDecimal"
android:layout_marginTop="16dp"/>
<Button 
android:id="@+id/buttonCalculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_marginTop="16dp"/> 
<TextView
android:id="@+id/textViewResult" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
</LinearLayout>

CODE IMPLEMENTATION IN MAIN ACTIVITY.JAVA.

Finally, Your Java code implementation for the BMI calculator in MainActivity.java:

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{ 
private EditText editTextWeight;
private EditText editTextHeight;
private Button buttonCalculate;
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextWeight = findViewById(R.id.editTextWeight);
editTextHeight = findViewById(R.id.editTextHeight);
buttonCalculate = findViewById(R.id.buttonCalculate);
textViewResult = findViewById(R.id.textViewResult);
buttonCalculate.setOnClickListener(new View.OnClickListener()
{
 @Override
public void onClick(View v)
{
calculateBMI(); 
}
});
}
private void calculateBMI()
{
String weightStr = editTextWeight.getText().toString();
String heightStr = editTextHeight.getText().toString();
if (weightStr.isEmpty())
{
editTextWeight.setError("Please enter your weight");
editTextWeight.requestFocus(); return;
}
if (heightStr.isEmpty())
{
editTextHeight.setError("Please enter your height");
editTextHeight.requestFocus();
return;
}
float weight = Float.parseFloat(weightStr);
float height = Float.parseFloat(heightStr) / 100;
float bmi = weight / (height * height);
String bmiResult = String.format("Your BMI is %.1f", bmi);
String bmiCategory = getBMICategory(bmi);
bmiResult = bmiResult + "\n" + bmiCategory;
textViewResult.setText(bmiResult);
}
private String getBMICategory(float bmi)
{
if (bmi < 18.5)
{ 
return "Underweight";
} 
else if
(bmi < 25)
{
return "Normal weight";
}
else if (bmi < 30) 
{
return "Overweight"; 
}
else
{
return "Obese"; 
} 
} 
} 

This code defines a MainActivity class that extends AppCompatActivity. The class includes private member variables for the EditText views for weight and height, the Button view for the calculate button, and the TextView view for the BMI result.
The onCreate() method initializes the member variables by finding them using their resource IDs. It also sets up an onClickListener for the calculate button that calls the calculateBMI() method when clicked.
The calculateBMI() method retrieves the weight and height values from the EditText views, checks if they are empty, converts them to floats, calculates the BMI value, and creates a string with the BMI result and category. It then sets the text of the result TextView to the BMI string.
The getBMICategory() method takes the calculated BMI value as an argument and returns a string with the corresponding BMI category based on the standard classification system.

Leave a Reply

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

Previous article

Lifecycle of Android Services