Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mybmicalculator16092022;
- 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.TextView;
- public class MainActivity extends AppCompatActivity {
- // 1
- private EditText etWeight, etHight;
- private Button btnCalculate;
- private TextView tvResult;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 0
- setContentView(R.layout.activity_main);
- // 3
- etWeight = findViewById(R.id.etWeight);
- etHight = findViewById(R.id.etHight);
- btnCalculate = findViewById(R.id.btnCalculate);
- tvResult = findViewById(R.id.tvResult);
- // 4 init
- tvResult.setText("");
- }
- public void calculateBmi(View view) {
- String weightStr = etWeight.getText().toString();
- String hightStr = etHight.getText().toString();
- double weight = Double.parseDouble(weightStr);
- double hight = Double.parseDouble(hightStr);
- double bmi = Math.round(weight / (hight * hight));
- if (bmi < 18.5) {
- tvResult.setText("The result is " + bmi + "\nUnderWeight");
- tvResult.setTextColor(Color.CYAN);
- } else if (bmi < 25) {
- tvResult.setText("The result is " + bmi + "\nNormal");
- tvResult.setTextColor(Color.GREEN);
- } else if (bmi < 30) {
- tvResult.setText("The result is " + bmi + "\nOverWeight");
- tvResult.setTextColor(Color.YELLOW);
- } else {
- tvResult.setText("The result is " + bmi + "\nObese");
- tvResult.setTextColor(Color.RED);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement