Creating a BMI Calculator Android App Tutorial
In this tutorial, we will be creating an Android app that calculates Body Mass Index (BMI) based height and weight entered by the user.
We will support both the metric unit system and the imperial unit system for calculating the BMI which allows the user to enter weight in either kilograms or pounds and the height in centimeters or feet and inches.
See the sample screenshots below for the Android app we will build.
activity_main.xml
step 1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/llColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".BmiActivity">
<EditText
android:id="@+id/edtWt"
android:layout_width="300dp"
android:layout_height="50dp"
android:background="@drawable/edtibg"
android:hint="@string/weight"
android:textColor="@color/black"
android:inputType="number"
android:paddingStart="10dp"
tools:ignore="RtlSymmetry" />
<EditText
android:id="@+id/edtFt"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:textColor="@color/black"
android:background="@drawable/edtibg"
android:hint="@string/height_ft"
android:inputType="number"
android:paddingStart="10dp" />
<EditText
android:id="@+id/edtIn"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:textColor="@color/black"
android:background="@drawable/edtibg"
android:hint="@string/height_in"
android:inputType="number"
android:paddingStart="10dp" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnCalculate"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:backgroundTint="@color/btnBg"
android:text="@string/bmicalculate"
android:textColor="@color/white"
android:textSize="16sp" />
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
android:id="@+id/txtCard"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:padding="5dp"
android:visibility="gone"
android:layout_marginTop="20dp">
<TextView
android:padding="5dp"
android:layout_gravity="center"
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:textSize="25sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
app:cardCornerRadius="10dp">
<ImageView
android:id="@+id/imgResult"
android:layout_width="300dp"
android:visibility="gone"
android:layout_height="200dp"
android:contentDescription="Image" />
</androidx.cardview.widget.CardView>
</LinearLayout>
MainActivity.java
step 2:
package com.sandhya.androidallproject2023;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.cardview.widget.CardView;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class BmiActivity extends AppCompatActivity {
EditText edtWt, edtFt, edtIn;
AppCompatButton btnCalculate;
TextView txtResult;
LinearLayout llColor;
ImageView imgResult;
CardView txtCard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
getActionBar();
initId();
initResult();
}
public void initId() {
edtWt = findViewById(R.id.edtWt);
edtFt = findViewById(R.id.edtFt);
edtIn = findViewById(R.id.edtIn);
btnCalculate = findViewById(R.id.btnCalculate);
txtResult = findViewById(R.id.txtResult);
llColor = findViewById(R.id.llColor);
imgResult = findViewById(R.id.imgResult);
txtCard = findViewById(R.id.txtCard);
}
public void initResult() {
btnCalculate.setOnClickListener(new View.OnClickListener() {
int wt, ft, in, totalIn;
double bmi, totalCm, totalM;
@Override
public void onClick(View v) {
wt = Integer.parseInt(edtWt.getText().toString());
ft = Integer.parseInt(edtFt.getText().toString());
in = Integer.parseInt(edtIn.getText().toString());
totalIn = ft * 12 + in;
totalCm = totalIn * 2.53;
totalM = totalCm / 100;
bmi = wt / (totalM * totalM);
if (bmi > 25) {
txtResult.setText(R.string.overweight);
llColor.setBackgroundColor(getResources().getColor(R.color.overWeight));
//imgResult.setBackgroundResource(R.drawable.overwights);
imgResult.setImageResource(R.drawable.overwights);
imgResult.setScaleType(ImageView.ScaleType.CENTER);
imgResult.setVisibility(View.VISIBLE);
txtCard.setVisibility(View.VISIBLE);
Toast.makeText(BmiActivity.this, R.string.overweight,
Toast.LENGTH_LONG).show();
} else if (bmi < 18) {
txtResult.setText(R.string.underweight);
llColor.setBackgroundColor(getResources().getColor(R.color.underWeight));
//imgResult.setBackgroundResource(R.drawable.underweight);
imgResult.setImageResource(R.drawable.underweight);
imgResult.setVisibility(View.VISIBLE);
txtCard.setVisibility(View.VISIBLE);
imgResult.setScaleType(ImageView.ScaleType.CENTER);
Toast.makeText(BmiActivity.this, R.string.underweight,
Toast.LENGTH_LONG).show();
} else {
txtResult.setText(R.string.healthy);
llColor.setBackgroundColor(getResources().getColor(R.color.healthy));
//imgResult.setBackgroundResource(R.drawable.healthy);
imgResult.setImageResource(R.drawable.healthy);
imgResult.setScaleType(ImageView.ScaleType.FIT_XY);
imgResult.setVisibility(View.VISIBLE);
txtCard.setVisibility(View.VISIBLE);
Toast.makeText(BmiActivity.this, R.string.healthy,
Toast.LENGTH_LONG).show();
}
}
});
}
}
Output:-
Post a Comment