-2

I am creating a simple BMI calculator to help with learning javascript. I have the following base code but have a couple of questions about how I could expand the capability.

  1. Is there a way that I can generate a picture that is displayed when a user hits "Calculate BMI"?

  2. Is there a way that I can create a version of this that when you hit "Calculate BMI" does not return the actual calculated BMI value but instead returns a word from a list of descriptive words. For example, I enter height and weight and then press "Calculate BMI" and the app displays "Wonderful" or "Beautiful" etc as the result?

Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.ssaurel.bmicalculator.MainActivity">

    <TextView
    android:text="@string/weight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:layout_marginTop="50dp"
    android:textSize="20sp"/>

    <EditText
    android:id="@+id/weight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:ems="6"
    android:inputType="number|numberDecimal"
    android:textSize="20sp"/>

    <TextView
    android:text="@string/height"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:layout_marginTop="50dp"
    android:textSize="20sp"/>

    <EditText
    android:id="@+id/height"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:ems="6"
    android:inputType="number|numberDecimal"
    android:textSize="20sp"/>

    <Button
    android:id="@+id/calc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="25dp"
    android:onClick="calculateBMI"
    android:text="@string/calculateBMI"
    />

    <TextView
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:layout_marginTop="25dp"
    android:textSize="20sp"/>

    </LinearLayout>

Main Activity:

    package com.ssaurel.bmicalculator;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

    private EditText height;
    private EditText weight;
    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    height = (EditText) findViewById(R.id.height);
    weight = (EditText) findViewById(R.id.weight);
    result = (TextView) findViewById(R.id.result);
    }

    public void calculateBMI(View v) {
    String heightStr = height.getText().toString();
    String weightStr = weight.getText().toString();

    if (heightStr != null && !"".equals(heightStr)
    && weightStr != null && !"".equals(weightStr)) {
    float heightValue = Float.parseFloat(heightStr) / 100;
    float weightValue = Float.parseFloat(weightStr);

    float bmi = weightValue / (heightValue * heightValue);

    displayBMI(bmi);
    }
    }

    private void displayBMI(float bmi) {
    String bmiLabel = "";

    if (Float.compare(bmi, 15f) <= 0) {
    bmiLabel = getString(R.string.very_severely_underweight);
    } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
    bmiLabel = getString(R.string.severely_underweight);
    } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
    bmiLabel = getString(R.string.underweight);
    } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
    bmiLabel = getString(R.string.normal);
    } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
    bmiLabel = getString(R.string.overweight);
    } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
    bmiLabel = getString(R.string.obese_class_i);
    } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
    bmiLabel = getString(R.string.obese_class_ii);
    } else {
    bmiLabel = getString(R.string.obese_class_iii);
    }

    bmiLabel = bmi + "\n\n" + bmiLabel;
    result.setText(bmiLabel);
    }
    }

Any assistance is much appreciated.

1 Answers1

-1

Use of statements to decide which message to display.

`if(BMI>value){
    Textbox.setText("message")
}
user145490
  • 27
  • 1
  • 6
  • Is there a way that I can define 20 or so words in a string and then randomly populate one of them each time someone presses "Calculate BMI"? – Standingbear123 Sep 22 '17 at 01:29
  • Define a number for each string you want and select a random number so that it in turn selects the string attached to that number. Also who downvotes an answer and doesn't offer another answer/ a comment to illustrate – user145490 Sep 22 '17 at 08:08