mmayoub

School, Bmi calculator, 18.02.2018

Feb 18th, 2018
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BMI Calculater  18.02.2018
  2. --------------------------
  3.  
  4. MainActivity.java
  5. --------------------
  6. package com.example.mohamadpc.bmiproject;
  7.  
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14.  
  15. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  16.     EditText etWeight, etHeight;
  17.     Button btnCalculate;
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.  
  24.         etHeight = (EditText) findViewById(R.id.etHeight);
  25.         etWeight = (EditText) findViewById(R.id.etWeight);
  26.         btnCalculate = (Button) findViewById(R.id.btnCalculate);
  27.  
  28.         btnCalculate.setOnClickListener(this);
  29.     }
  30.  
  31.     @Override
  32.     public void onClick(View view) {
  33.         String w = etWeight.getText().toString();
  34.         double weight = Double.parseDouble(w);
  35.  
  36.         double height = Double.parseDouble(etHeight.getText().toString());
  37.  
  38.         double bmi = weight / (height * height);
  39.  
  40.         //Toast.makeText(this, "bmi=" + bmi, Toast.LENGTH_LONG).show();
  41.  
  42.         Intent i = new Intent(this, ResultActivity.class);
  43.  
  44.         // send data bu intent
  45.         i.putExtra("TheResult", bmi);
  46.  
  47.         startActivity(i);
  48.  
  49.     }
  50. }
  51.  
  52.  
  53. ResultActivity.java
  54. --------------------
  55. package com.example.mohamadpc.bmiproject;
  56.  
  57. import android.content.Intent;
  58. import android.os.Bundle;
  59. import android.support.v7.app.AppCompatActivity;
  60. import android.widget.TextView;
  61.  
  62. public class ResultActivity extends AppCompatActivity {
  63.     TextView tvBmi;
  64.     TextView tvMessage;
  65.  
  66.     @Override
  67.     protected void onCreate(Bundle savedInstanceState) {
  68.         super.onCreate(savedInstanceState);
  69.         setContentView(R.layout.activity_result);
  70.  
  71.         tvBmi = findViewById(R.id.tvBmi);
  72.         tvMessage = findViewById(R.id.tvMessage);
  73.  
  74.         // get data from intent
  75.         Intent i = this.getIntent();
  76.         double bmi = i.getDoubleExtra("TheResult", 0);
  77.  
  78.         // display result
  79.         tvBmi.setText("BMI = " + bmi);
  80.  
  81.         String message;
  82.         if (bmi < 20) {
  83.             message = "You need to eat more!";
  84.         } else if (bmi < 25) {
  85.             message = "Normal weight";
  86.         } else if (bmi < 30) {
  87.             message = "Over weight";
  88.         } else if (bmi < 35) {
  89.             message = "You should have a diet!";
  90.         } else {
  91.             message = "You should visit a doctor!";
  92.         }
  93.  
  94.         tvMessage.setText(message);
  95.  
  96.     }
  97. }
  98.  
  99.  
  100. activity_main.xml
  101. -------------------
  102. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  103.     android:layout_width="match_parent"
  104.     android:layout_height="match_parent"
  105.     android:orientation="vertical">
  106.  
  107.     <TextView
  108.         android:layout_width="match_parent"
  109.         android:layout_height="wrap_content"
  110.         android:background="#ecf454"
  111.         android:gravity="center"
  112.         android:text="Bmi Calculator"
  113.         android:textColor="#f23333"
  114.         android:textSize="32sp" />
  115.  
  116.     <EditText
  117.         android:id="@+id/etWeight"
  118.         android:layout_width="match_parent"
  119.         android:layout_height="wrap_content"
  120.         android:layout_marginTop="16dp"
  121.         android:gravity="center"
  122.         android:hint="Weight (kg)"
  123.         android:textSize="28sp" />
  124.  
  125.     <EditText
  126.         android:id="@+id/etHeight"
  127.         android:layout_width="match_parent"
  128.         android:layout_height="wrap_content"
  129.         android:layout_marginTop="16dp"
  130.         android:gravity="center"
  131.         android:hint="Height (m)"
  132.         android:textSize="28sp" />
  133.  
  134.     <Button
  135.         android:id="@+id/btnCalculate"
  136.         android:layout_width="match_parent"
  137.         android:layout_height="wrap_content"
  138.         android:text="Calculate BMI"
  139.         android:textSize="22sp" />
  140.  
  141. </LinearLayout>
  142.  
  143. activity_result.xml
  144. -------------------
  145. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  146.     android:layout_width="match_parent"
  147.     android:layout_height="match_parent"
  148.     android:orientation="vertical">
  149.  
  150.     <TextView
  151.         android:id="@+id/tvBmi"
  152.         android:layout_width="match_parent"
  153.         android:layout_height="wrap_content"
  154.         android:gravity="center"
  155.         android:text="stam dogma"
  156.         android:textSize="32sp" />
  157.  
  158.     <TextView
  159.         android:id="@+id/tvMessage"
  160.         android:layout_width="match_parent"
  161.         android:layout_height="wrap_content"
  162.         android:gravity="center"
  163.         android:text="stam dogma"
  164.         android:textSize="32sp" />
  165.  
  166.     <ImageView
  167.         android:id="@+id/imgImage"
  168.         android:layout_width="match_parent"
  169.         android:layout_height="match_parent"
  170.         android:layout_weight="1"
  171.         android:background="@color/colorPrimary"
  172.         android:gravity="center"
  173.         android:scaleType="fitXY"
  174.         android:src="@drawable/sun" />
  175. </LinearLayout>
RAW Paste Data