Tugas 3: Body Mass Index Calculator
Tugas 3: Body Mass Index Calculator
APLIKASI MOBILE
TUGAS 3
Body Mass Index Calculator
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:text="BMI Calculator "
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="Yout BMI"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/BMIResult"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/BMIResult" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="WEIGHT (KG)"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/weightInput"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/weightInput" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="HEIGHT (M)"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/heightInput"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/weightInput" />
<TextView
android:id="@+id/BMICategory"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:gravity="center_horizontal"
android:text="Your BMI category appears here"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Calculate my BMI"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/heightInput"
app:layout_constraintTop_toBottomOf="@+id/heightInput" />
<EditText
android:id="@+id/weightInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView1" />
<EditText
android:id="@+id/heightInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/weightInput" />
<EditText
android:id="@+id/BMIResult"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button3" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.prak_basoarfanefendy_172263_o;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
Button button = (Button)findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText heightText = findViewById(R.id.heightInput);
String heightStr = heightText.getText().toString();
double height = Double.parseDouble(heightStr);
final EditText weightText = findViewById(R.id.weightInput);
String weightStr = weightText.getText().toString();
double weight = Double.parseDouble(weightStr);
double BMI = (weight)/(height*height);
DecimalFormat df = new DecimalFormat("#.#");
double BMI_trimmed = Double.parseDouble(df.format(BMI));
final EditText BMIResult = findViewById(R.id.BMIResult);
BMIResult.setText(Double.toString(BMI_trimmed));
String BMI_Cat;
if (BMI < 15)
BMI_Cat = "Very severely underweight";
else if (BMI < 16)
BMI_Cat = "Severely underweight";
else if (BMI < 18.5)
BMI_Cat = "Underweight";
else if (BMI < 25)
BMI_Cat = "Normal";
else if (BMI < 30)
BMI_Cat = "Overweight";
else if (BMI < 35)
BMI_Cat = "Obese Class 1 - Moderately Obese";
else if (BMI < 40)
BMI_Cat = "Obese Class 2 - Severely Obese";
else
BMI_Cat = "Obese Class 3 - Very Severely Obese";
final TextView BMICategory =
findViewById(R.id.BMICategory);
BMICategory.setText(BMI_Cat);
}
});
}
}