How to create a BMI( Body Mass Index) calculating App

 


Splash Screen Xml code:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity2">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/bmi" />


</androidx.constraintlayout.widget.ConstraintLayout>
Splash Screen Java Code:
package com.programmingwithus786;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i=new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();

}
},3000);
}
}
Main BMI App XML code:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/layout"
android:orientation="vertical"
tools:context=".MainActivity">


<EditText
android:id="@+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Your Weight "
android:ems="15"
android:inputType="number" />

<EditText
android:id="@+id/heightInFeet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Your Height (In feet)"
android:ems="15"
android:inputType="number" />

<EditText
android:id="@+id/heightInInches"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Your Height (In inches)"
android:ems="15"
android:inputType="number" />

<Button
android:id="@+id/calculateBMI"
android:layout_width="312dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Calculate body Mass " />

<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Result" />
</LinearLayout>
Main BMI App Java code:
package com.programmingwithus786;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
EditText weight,heightInFeet,heightInInches;
TextView result;
Button calculateBMI;
LinearLayout layout;


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

weight=findViewById(R.id.weight);
heightInFeet=findViewById(R.id.heightInFeet);
heightInInches=findViewById(R.id.heightInInches);
result=findViewById(R.id.result);
calculateBMI=findViewById(R.id.calculateBMI);
layout=findViewById(R.id.layout);

calculateBMI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

int w8=Integer.parseInt(weight.getText().toString());
int ft=Integer.parseInt(heightInFeet.getText().toString());
int in=Integer.parseInt(heightInInches.getText().toString());

int totalInches=ft*12 +in;
double totalcm=totalInches*2.53;
double totalm=totalcm/100;

double bmi =w8/(totalm*totalm);
if(bmi>25){
result.setText("You're OverWeight !");
layout.setBackgroundColor(getResources().getColor(R.color.red));
}else if(bmi<18){
result.setText("You're UnderWeight !");
layout.setBackgroundColor(getResources().getColor( R.color.yellow));
}else{
result.setText("You're Healthy!");
layout.setBackgroundColor(getResources().getColor( R.color.green));

}



}
});

}
}


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.