0% found this document useful (0 votes)
4 views4 pages

Package Com

The document contains an Android application code for calculating Body Mass Index (BMI). It includes user input fields for weight and height, a button to trigger the calculation, and a display for the result. The BMI is calculated based on user input, and categories are assigned based on the computed value.

Uploaded by

pridetumboyi506
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Package Com

The document contains an Android application code for calculating Body Mass Index (BMI). It includes user input fields for weight and height, a button to trigger the calculation, and a display for the result. The BMI is calculated based on user input, and categories are assigned based on the computed value.

Uploaded by

pridetumboyi506
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.example.

bodymassindex;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


Button button;
EditText weightInput, heightInput;
TextView textResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weightInput = findViewById(R.id.weightInput);
heightInput = findViewById(R.id.heightInput);
button = findViewById(R.id.button);
textResult = findViewById(R.id.textResult);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculateBMI();
}
});
}
public void calculateBMI(){
String weightString = weightInput.getText().toString();
String heightString = heightInput.getText().toString();
if(!weightString.isEmpty() && !heightString.isEmpty()) {
double weight = Double.parseDouble(heightString);
double height = Double.parseDouble(weightString);
double BMI = weight / (height * height);
String category = "Nothing Entered";
if (BMI < 15)
category = "underweight";
else if (BMI <= 16)
category = "Severely underweight";
textResult.setText(String.format("BMI:%.2f\nCategory:%s", BMI, category));
}
<EditText
android:id="@+id/heightInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Name"
tools:layout_editor_absoluteX="185dp"
tools:layout_editor_absoluteY="289dp" />

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/bodymassindex"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.473"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="120dp"
android:layout_height="45dp"
android:layout_marginStart="16dp"
android:gravity="center"
android:text="@string/weight"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="114dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="120dp"
android:layout_height="45dp"
android:layout_marginStart="16dp"
android:gravity="center"
android:text="@string/height"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="289dp" />

<Button
android:id="@+id/button"
android:layout_width="120dp"
android:layout_height="45dp"
android:background="@android:color/holo_blue_light"
android:text="@string/getbmi"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="434dp" />

<TextView
android:id="@+id/textView5"
android:layout_width="120dp"
android:layout_height="45dp"
android:layout_marginStart="16dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="100dp"
android:gravity="center"
android:text="@string/result"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textResult"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<TextView
android:id="@+id/textResult"
android:layout_width="120dp"
android:layout_height="45dp"
android:layout_marginStart="16dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="100dp"
android:gravity="center"
android:text="Result"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/button" />

<EditText
android:id="@+id/weightInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Name"
tools:layout_editor_absoluteX="175dp"
tools:layout_editor_absoluteY="111dp" />

}
}

You might also like