Advertisement
mmayoub

MainActivity.java

Aug 28th, 2021
1,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.     // 1
  14.     EditText etHeight;
  15.     EditText etWeight;
  16.     Button btnCalculate;
  17.     TextView tvBmi;
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.  
  24.         // 2
  25.         etHeight = findViewById(R.id.etHeight);
  26.         etWeight=findViewById(R.id.etWeight);
  27.         btnCalculate = findViewById(R.id.btnCalculate);
  28.         tvBmi = findViewById(R.id.tvBmi);
  29.     }
  30.  
  31.     public void calaculate(View view) {
  32.         String stw = etWeight.getText().toString();
  33.         if (stw.length()>0) {
  34.             double weight = Double.parseDouble(stw);
  35.  
  36.         double height = Double.parseDouble(etHeight.getText().toString());
  37.  
  38.         double bmi = weight / Math.pow(height,2);
  39.         tvBmi.setText(bmi+"");
  40.  
  41.         if (bmi>=20 && bmi<=24) {
  42.             Toast.makeText(this,"Very good",Toast.LENGTH_LONG).show();
  43.         }
  44.         }
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement