Create a BMI Calculator using Android Studio

start a new project and create the view as following and use the given code
You can download this app from the Amazon app store 
Click here to download the app



Get the full project from Click here to view git hub






//---------------------------


<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:id="@+id/lblWeight"
android:layout_marginTop="60dp"
android:text="WEIGHT"
android:textSize="25dp"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="100dp"
android:id="@+id/txtErrorWeight"
android:textColor="@color/error"
/>
<EditText
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="200dp"
android:id="@+id/txtWeightUp"
android:layout_marginTop="60dp"
android:maxLength="3"
android:gravity="center"
/>
<EditText
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="200dp"
android:id="@+id/txtWeight"
android:layout_marginTop="60dp"
android:maxLength="3"
android:gravity="center"
/>
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:id="@+id/lblHeight"
android:layout_marginTop="130dp"
android:text="HEIGHT"
android:textSize="25dp"
android:gravity="center"
/>
<EditText
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="240dp"
android:id="@+id/txtHeight"
android:layout_marginTop="130dp"
android:maxLength="3"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="190dp"
android:id="@+id/txtErrorHeight"
android:textColor="@color/error"
/>
<EditText
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:id="@+id/txtHeightFt"
android:layout_marginTop="130dp"
android:maxLength="3"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="ft"
android:textSize="30dp"
android:layout_marginLeft="220dp"
android:id="@+id/tFt"
android:layout_marginTop="130dp"
android:maxLength="3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="In"
android:textSize="30dp"
android:layout_marginLeft="360dp"
android:layout_marginTop="130dp"
android:maxLength="3"
/>
<Button
android:layout_width="350dp"
android:layout_height="wrap_content"
android:id="@+id/btnCalculateBmi"
android:layout_marginTop="250dp"
android:layout_marginLeft="30dp"
android:text="CALCULATE"
android:textSize="30dp"
android:background="@color/newOne"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginTop="320dp"
android:layout_marginLeft="30dp"
android:scaleY="10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="low weight"
android:layout_marginTop="390dp"
android:layout_marginLeft="30dp"
android:textSize="20dp"
android:textColor="@color/error"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="good"
android:layout_marginTop="390dp"
android:layout_marginLeft="140dp"
android:textSize="20dp"
android:textColor="@color/good"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="over height"
android:layout_marginTop="390dp"
android:layout_marginLeft="290dp"
android:textSize="20dp"
android:textColor="@color/error"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.0"
android:layout_marginTop="430dp"
android:layout_marginLeft="170dp"
android:textSize="50dp"
android:id="@+id/txtBmi"
android:textColor="@color/error"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From your weight"
android:layout_marginTop="500dp"
android:layout_marginLeft="110dp"
android:textSize="20dp"
android:textColor="@color/normalText"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.0"
android:layout_marginTop="525dp"
android:layout_marginLeft="180dp"
android:textSize="20dp"
android:id="@+id/txtWeightAdjust"
android:textColor="@color/normalText"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="you have to increase"
android:id="@+id/lblWeightUp"
android:layout_marginTop="550dp"
android:layout_marginLeft="100dp"
android:textSize="20dp"
android:textColor="@color/normalText"
android:visibility="invisible"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have to decrease"
android:id="@+id/lblWeightDown"
android:layout_marginTop="550dp"
android:layout_marginLeft="100dp"
android:textSize="20dp"
android:textColor="@color/normalText"
android:visibility="invisible"
/>
</RelativeLayout> use this link to get code Get the code from git hub
Create the MainActivity.class as following

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
EditText height,weight,ft;
ProgressBar pbarResult;
TextView eW,eH,up,down,adjusted,lblBmi;
double w,h;
String we,he,wP,hP;
double feets;
double adjust;
int progressStatus = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weight=(EditText)findViewById(R.id.txtWeight);
height=(EditText)findViewById(R.id.txtHeight);
eW=(TextView) findViewById(R.id.txtErrorWeight);
eH=(TextView) findViewById(R.id.txtErrorHeight);
ft=(EditText)findViewById(R.id.txtHeightFt);
up=(TextView)findViewById(R.id.lblWeightUp);
down=(TextView)findViewById(R.id.lblWeightDown);
adjusted=(TextView)findViewById(R.id.txtWeightAdjust);
lblBmi=(TextView)findViewById(R.id.txtBmi);
final Button calculateBmi =(Button)findViewById(R.id.btnCalculateBmi);
pbarResult =(ProgressBar)findViewById(R.id.progressBar2);

calculateBmi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
down.setVisibility(View.INVISIBLE);
up.setVisibility(View.INVISIBLE);
pbarResult.setProgress(0);
pbarResult.getProgressDrawable().setColorFilter(
Color.parseColor("#3F51B5"), android.graphics.PorterDuff.Mode.SRC_IN);
verify();
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast toast=Toast. makeText(getApplicationContext(),"nothing selected", Toast. LENGTH_SHORT);
// toast. setMargin(50,50);
toast. show();
}
public void verify(){
if(weight.getText().toString().equals("")){
eW.setText("Please enter a weight");

if(ft.getText().toString().equals("") && height.getText().toString().equals("")){
eH.setText("please enter a height");
}
}else{
try{
convert();
}catch (Exception e){
Toast toast1=Toast. makeText(getApplicationContext(),"ex in verify else", Toast. LENGTH_SHORT);
toast1. show();
}
}
}
public void convert(){


w=Integer.parseInt(weight.getText().toString());
h=Integer.parseInt(height.getText().toString());
feets= (Double.parseDouble(ft.getText().toString())/3.281);
Toast toast2=Toast. makeText(getApplicationContext(),"feets"+feets, Toast. LENGTH_SHORT);

hP="in";
try {
if( hP.equals("in")){
h=h/39.37;
calculate(w,(feets+h));
}else{
h=h/100;
calculate(w,(feets+h));
}
}catch (Exception e){
Toast toast1=Toast. makeText(getApplicationContext(),"ex in convert", Toast. LENGTH_SHORT);
toast1. show();
}
}
public void adjustCalculate(double x){
double wei;
if(x<25){
wei =25-x;
up.setVisibility(View.VISIBLE);
}else {
wei=x-25;
down.setVisibility(View.VISIBLE);
}
adjust=wei*(feets+h)*(feets+h);
adjusted.setText(new DecimalFormat("#.0").format(adjust));
}
public void calculate(double w,double h){
double bmi=w/(h*h);
// Toast.makeText(MainActivity.this,"bmi is : "+bmi,Toast.LENGTH_SHORT).show();
lblBmi.setText(new DecimalFormat("#.0").format(bmi));
double x=((bmi-15)/15)*100;
if(bmi<=18.5){
pbarResult.getProgressDrawable().setColorFilter(
Color.parseColor("#0A0EC9"), android.graphics.PorterDuff.Mode.SRC_IN);
}else if(18.5<bmi && bmi<=25.0){
pbarResult.getProgressDrawable().setColorFilter(
Color.parseColor("#5FFF3B"), android.graphics.PorterDuff.Mode.SRC_IN);
}else{
pbarResult.getProgressDrawable().setColorFilter(
Color.parseColor("#E00606"), android.graphics.PorterDuff.Mode.SRC_IN);
}
adjustCalculate(Double.parseDouble(new DecimalFormat("#.0").format(bmi)));
showProgressBar(x);
}
public void showProgressBar(final double x){

final Handler handler = new Handler();
new Thread(new Runnable() {
public void run() {
while (progressStatus < x) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
pbarResult.setProgress(progressStatus);

}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
} Get the full project from Github Click here