Advertisement
mmayoub

BmiApplication_java

Sep 5th, 2021
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10.  
  11. import androidx.appcompat.app.AppCompatActivity;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.     // 1
  15.     EditText etHeight;
  16.     EditText etWeight;
  17.     Button btnCalculate;
  18.     TextView tvBmi;
  19.     ImageView imageView;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.  
  26.         // 2
  27.         etHeight = findViewById(R.id.etHeight);
  28.         etWeight = findViewById(R.id.etWeight);
  29.         btnCalculate = findViewById(R.id.btnCalculate);
  30.         tvBmi = findViewById(R.id.tvBmi);
  31.         imageView = findViewById(R.id.imageView);
  32.     }
  33.  
  34.     public void calaculate(View view) {
  35.         String stw = etWeight.getText().toString();
  36.         String sth = etHeight.getText().toString();
  37.  
  38.         if (stw.length() > 0 && sth.length() > 0) {
  39.             double weight = Double.parseDouble(stw);
  40.  
  41.             double height = Double.parseDouble(sth);
  42.  
  43.             double bmi = weight / Math.pow(height, 2);
  44.  
  45.             if (bmi >= 20 && bmi <= 24) {
  46.                 Toast.makeText(this, "Very good", Toast.LENGTH_LONG).show();
  47.             }
  48.  
  49.  
  50.             String msg = "";
  51.             int imgId;
  52.             if (bmi < 18) {
  53.                 msg = "too fat";
  54.                 imgId = R.drawable.bmi1;
  55.             } else if (bmi <= 24) {
  56.                 msg = "normal";
  57.                 imgId = R.drawable.bmi2;
  58.             } else if (bmi < 30) {
  59.                 msg = "fat";
  60.                 imgId = R.drawable.bmi3;
  61.             } else if (bmi <= 40) {
  62.                 msg = "too fat";
  63.                 imgId = R.drawable.bmi4;
  64.             } else {
  65.                 msg = "very very fat";
  66.                 imgId = R.drawable.bmi5;
  67.             }
  68.  
  69.             tvBmi.setText((int) bmi + " (" + msg + ")");
  70.  
  71.  
  72.             imageView.setImageResource(imgId);
  73.             imageView.setVisibility(View.VISIBLE);
  74.         } else {
  75.             Toast.makeText(this, "Error: Weight or Height is empty!", Toast.LENGTH_LONG).show();
  76.         }
  77.     }
  78.  
  79.     public void exit(View view) {
  80.         finish();
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement