Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- public class MainActivity extends AppCompatActivity {
- // 1
- EditText etHeight;
- EditText etWeight;
- Button btnCalculate;
- TextView tvBmi;
- ImageView imageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 2
- etHeight = findViewById(R.id.etHeight);
- etWeight = findViewById(R.id.etWeight);
- btnCalculate = findViewById(R.id.btnCalculate);
- tvBmi = findViewById(R.id.tvBmi);
- imageView = findViewById(R.id.imageView);
- }
- public void calaculate(View view) {
- String stw = etWeight.getText().toString();
- String sth = etHeight.getText().toString();
- if (stw.length() > 0 && sth.length() > 0) {
- double weight = Double.parseDouble(stw);
- double height = Double.parseDouble(sth);
- double bmi = weight / Math.pow(height, 2);
- if (bmi >= 20 && bmi <= 24) {
- Toast.makeText(this, "Very good", Toast.LENGTH_LONG).show();
- }
- String msg = "";
- int imgId;
- if (bmi < 18) {
- msg = "too fat";
- imgId = R.drawable.bmi1;
- } else if (bmi <= 24) {
- msg = "normal";
- imgId = R.drawable.bmi2;
- } else if (bmi < 30) {
- msg = "fat";
- imgId = R.drawable.bmi3;
- } else if (bmi <= 40) {
- msg = "too fat";
- imgId = R.drawable.bmi4;
- } else {
- msg = "very very fat";
- imgId = R.drawable.bmi5;
- }
- tvBmi.setText((int) bmi + " (" + msg + ")");
- imageView.setImageResource(imgId);
- imageView.setVisibility(View.VISIBLE);
- } else {
- Toast.makeText(this, "Error: Weight or Height is empty!", Toast.LENGTH_LONG).show();
- }
- }
- public void exit(View view) {
- finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement