App Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.BMIApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
1. Java Code:
package com.example.bmiapp;
import static com.example.bmiapp.R.color.colorO;
import androidx.appcompat.app.AppCompatActivity;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edtWeight, edtHeightFt, edtHeightIn;
Button btnCalculate;
TextView txtResult;
LinearLayout llMain;
edtWeight = findViewById(R.id.edtWeight);
edtHeightFt = findViewById(R.id.edtHeightFt);
edtHeightIn = findViewById(R.id.edtHeightIn);
btnCalculate = findViewById(R.id.btnCalculate);
txtResult = findViewById(R.id.txtResult);
llMain = findViewById(R.id.llMain);
btnCalculate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int wt = Integer.parseInt(edtWeight.getText().toString());
int ft = Integer.parseInt(edtHeightFt.getText().toString());
int in = Integer.parseInt(edtHeightIn.getText().toString());
int totalIn = ft*12 + in;
double totalcm = totalIn*2.53;
double totalM = totalcm/100;
double bmi = wt/(totalM*totalM);
if(bmi>25){
txtResult.setText("You'r OverWeight!");
llMain.setBackgroundColor(getResources().getColor(R.color.colorO));
} else if (bmi<18) {
txtResult.setText("You'r underWeight!");
llMain.setBackgroundColor(getResources().getColor(R.color.colorU));
}else {
txtResult.setText("You'r Healthy!");
llMain.setBackgroundColor(getResources().getColor(R.color.colorH));
}
}
});
}
}
2. XML Layout 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:orientation="vertical"
android:gravity="center"
android:id="@+id/llMain"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint_wt"
android:id="@+id/edtWeight"
android:inputType="number"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint_hF"
android:id="@+id/edtHeightFt"
android:inputType="number"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint_hI"
android:id="@+id/edtHeightIn"
android:inputType="number"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="11dp"
android:text="Calculate BMI"
android:id="@+id/btnCalculate"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:text="Result"
android:textSize="21sp"
android:textStyle="bold"
android:layout_marginTop="11dp"/>
<TextView
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="11dp"
android:gravity="bottom"
android:text="Sadam Hussain Mahar"
android:textSize="28sp"
android:textStyle="bold" />
</LinearLayout>