HeatKai C HeatKai C - 1 month ago 16
Android Question

BMI calculation, output using alertdialog

I have a problem with my current source code. I'm quite new with this. It's about BMI calculation and will show output by using alertdialog. I have put the condition already but the output is always "You are OVERWEIGHT" on else condition even though my input was legit supposed to be IDEAL output.

The activity got no error in running but I got a feeling it's the calculation problem that I don't know. I am really sorry if this is a duplicate question.

public class BMIcalcu extends Activity{
EditText weight, height;
final Context context = this;
Button calculate;
float result, height1, weight1;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_calcu);

height = (EditText)findViewById(R.id.editText1);
weight = (EditText)findViewById(R.id.editText2);
calculate = (Button)findViewbyId(R.id.btn_bmi);

String str1 = height.getText().toString();
String str2 = weight.getText().toString();

try{
height1 = Float.parseFloat(str1)/100;
weight1 = Float.parseFloat(str2);
}catch(NumberFormatException nfe){
}

calculate.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v){
result = weight1 / (height1*height1);
if(result<18.5){

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

alertDialogBuilder.setTitle("Your BMI");

alertDialogBuilder
.setMessage("You are UNDERWEIGHT! START EATING!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){

dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

}
else if(result<25){

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

alertDialogBuilder.setTitle("Your BMI");

alertDialogBuilder
.setMessage("You are on your IDEAL!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){

dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
else{


AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

alertDialogBuilder.setTitle("Your BMI");

alertDialogBuilder
.setMessage("You are OVERWEIGHT! START EXERCISING!")
.setCancelable(false)
.setPositiveButton("List", new DialogInterface.OnClickLister(){
public void onClick(DialogInterface dialog, int id){
Intent intent = new Intent(BMIcalcu.this, list.class);
startActivity(intent);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){

dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
}
}


This is the xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_gravity="center"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:inputType="text|number"
android:hint="Height (Centimeter)"
android:ems="10"
android:id="@+id/editText1" />

<EditText
android:layout_gravity="center"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:inputType="text|number"
android:hint="Weight (Kilogram)"
android:ems="10"
android:id="@+id/editText2"/>

<Button
android:id="@+id/btn_bmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="@+id/editText2"
android:text="Calculate"/>
</LinearLayout>

Answer

Move height and weight assignment inside onClick().

calculate.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
  String str1 = height.getText().toString();
  String str2 = weight.getText().toString();

  try{
    height1 = Float.parseFloat(str1)/100;
    weight1 = Float.parseFloat(str2);
    }catch(NumberFormatException nfe){
      }

            result = weight1 / (height1*height1);
            String msg = "";
            if(result<18.5){
                msg = "You are UNDERWEIGHT! START EATING!";
            }
            else if(result<25){
                msg = "You are on your IDEAL!";
            }
            else{
                msg = "You are UNDERWEIGHT! START EATING!";
            }

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

            alertDialogBuilder.setTitle("Your BMI");

            alertDialogBuilder
                    .setMessage(msg)
                    .setCancelable(false)
                    .setPositiveButton("List", new DialogInterface.OnClickLister(){
                        public void onClick(DialogInterface dialog, int id){
                            Intent intent = new Intent(BMIcalcu.this, list.class);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton("Back", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int id){

                            dialog.cancel();
                        }
                    });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    });
Comments