Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BMI Calculater 18.02.2018
- --------------------------
- MainActivity.java
- --------------------
- package com.example.mohamadpc.bmiproject;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- EditText etWeight, etHeight;
- Button btnCalculate;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etHeight = (EditText) findViewById(R.id.etHeight);
- etWeight = (EditText) findViewById(R.id.etWeight);
- btnCalculate = (Button) findViewById(R.id.btnCalculate);
- btnCalculate.setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- String w = etWeight.getText().toString();
- double weight = Double.parseDouble(w);
- double height = Double.parseDouble(etHeight.getText().toString());
- double bmi = weight / (height * height);
- //Toast.makeText(this, "bmi=" + bmi, Toast.LENGTH_LONG).show();
- Intent i = new Intent(this, ResultActivity.class);
- // send data bu intent
- i.putExtra("TheResult", bmi);
- startActivity(i);
- }
- }
- ResultActivity.java
- --------------------
- package com.example.mohamadpc.bmiproject;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.TextView;
- public class ResultActivity extends AppCompatActivity {
- TextView tvBmi;
- TextView tvMessage;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_result);
- tvBmi = findViewById(R.id.tvBmi);
- tvMessage = findViewById(R.id.tvMessage);
- // get data from intent
- Intent i = this.getIntent();
- double bmi = i.getDoubleExtra("TheResult", 0);
- // display result
- tvBmi.setText("BMI = " + bmi);
- String message;
- if (bmi < 20) {
- message = "You need to eat more!";
- } else if (bmi < 25) {
- message = "Normal weight";
- } else if (bmi < 30) {
- message = "Over weight";
- } else if (bmi < 35) {
- message = "You should have a diet!";
- } else {
- message = "You should visit a doctor!";
- }
- tvMessage.setText(message);
- }
- }
- activity_main.xml
- -------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#ecf454"
- android:gravity="center"
- android:text="Bmi Calculator"
- android:textColor="#f23333"
- android:textSize="32sp" />
- <EditText
- android:id="@+id/etWeight"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="16dp"
- android:gravity="center"
- android:hint="Weight (kg)"
- android:textSize="28sp" />
- <EditText
- android:id="@+id/etHeight"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="16dp"
- android:gravity="center"
- android:hint="Height (m)"
- android:textSize="28sp" />
- <Button
- android:id="@+id/btnCalculate"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Calculate BMI"
- android:textSize="22sp" />
- </LinearLayout>
- activity_result.xml
- -------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tvBmi"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="stam dogma"
- android:textSize="32sp" />
- <TextView
- android:id="@+id/tvMessage"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="stam dogma"
- android:textSize="32sp" />
- <ImageView
- android:id="@+id/imgImage"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="@color/colorPrimary"
- android:gravity="center"
- android:scaleType="fitXY"
- android:src="@drawable/sun" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement