java - (Android)Trying to understand why my method will not compute/display the correct value in the textview -
my method called (calculate bmi) inside of activity supposed compute , display bmi (based on formula inside code), , display value inside of textview. application runs correctly, value displayed zero. struggling figure out if problem lies in formula (unlikely) or if way passing value incorrect. appreciated. code below
.java file
import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.edittext; import android.widget.textview; import android.view.view; import android.widget.toast; public class health extends actionbaractivity { edittext bloodpressure; edittext cholesterol; int weight; int height; edittext heightstring; edittext age; textview bmitext; edittext weightstring; mydbhandler dbhandler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_health); weightstring = (edittext) findviewbyid(r.id.weighttextbox); heightstring =(edittext) findviewbyid(r.id.heighttextbox); bmitext = (textview) findviewbyid(r.id.bmitextview); bloodpressure = (edittext) findviewbyid(r.id.bloodpressuretextbox); cholesterol = (edittext) findviewbyid(r.id.cholesteroltextbox); age = (edittext) findviewbyid(r.id.agetextbox); dbhandler = new mydbhandler(this, null, null, 1); } public void calculatebmi(view view) { weight = integer.parseint(weightstring.gettext().tostring()); height = integer.parseint(heightstring.gettext().tostring()); int bmi = 703 * (weight/(height*height)); bmitext.settext("your calculated bmi is: " + integer.tostring(bmi)); toast.maketext(health.this, " info saved ", toast.length_short).show(); healthinfo healthinfo = new healthinfo(bloodpressure.gettext().tostring(),cholesterol.gettext().tostring(),heightstring.gettext().tostring(),weightstring.gettext().tostring(),age.gettext().tostring()); dbhandler.addhealth(healthinfo); } public void calorieclick(view view){ intent = new intent(this, calorietracker.class); startactivity(i); } public void opentips(view view){ intent = new intent(this, healthtipsactivity.class); startactivity(i); } public void openplans(view view){ intent = new intent(this, dietplansactivity.class); startactivity(i); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_health, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
.xml file
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="seniorproject.broncos.broncowellnessapp.health" android:background="#006699"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/blood_pressure_text" android:id="@+id/bloodpressuretextview" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/cholesterol_text" android:id="@+id/cholesteroltextview" android:layout_below="@+id/bloodpressuretextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="35dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/weight_text" android:id="@+id/weighttextview" android:layout_below="@+id/cholesteroltextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="35dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/height_text" android:id="@+id/heighttextview" android:layout_below="@+id/weighttextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="35dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/age_text" android:id="@+id/agetextview" android:layout_below="@+id/heighttextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="35dp" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="text" android:ems="10" android:width="150dp" android:id="@+id/bloodpressuretextbox" android:layout_below="@+id/bloodpressuretextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="number" android:ems="10" android:width="150dp" android:id="@+id/cholesteroltextbox" android:layout_below="@+id/cholesteroltextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="number" android:ems="10" android:width="150dp" android:id="@+id/weighttextbox" android:layout_below="@+id/weighttextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="number" android:ems="10" android:width="150dp" android:id="@+id/heighttextbox" android:layout_below="@+id/heighttextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="number" android:ems="10" android:width="150dp" android:id="@+id/agetextbox" android:layout_below="@+id/agetextview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/diet_plans_button_text" android:onclick="openplans" android:id="@+id/dietplansbutton" android:layout_below="@+id/agetextbox" android:layout_margintop="40dp" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/calorie_tracker_button" android:id="@+id/calorieaccessbutton" android:layout_below="@+id/dietplansbutton" android:onclick ="calorieclick" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/tips_button_text" android:id="@+id/tipsbutton" android:onclick="opentips" android:layout_below="@+id/calorieaccessbutton" android:layout_alignright="@+id/calorieaccessbutton" android:layout_alignend="@+id/calorieaccessbutton" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bmi_button_text" android:id="@+id/bmibutton" android:onclick="calculatebmi" android:layout_alignbottom="@+id/cholesteroltextbox" android:layout_alignright="@+id/dietplansbutton" android:layout_alignend="@+id/dietplansbutton" android:layout_marginright="36dp" android:layout_marginend="36dp" android:background="#ff00b812" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="push button compute bmi" android:id="@+id/bmitextview" android:layout_alignbottom="@+id/weighttextbox" android:layout_alignleft="@+id/bmibutton" android:layout_alignstart="@+id/bmibutton" /> </relativelayout>
your weight
, height
variables int
. problem. change them float
or double
currently, doing integer division causing result truncated follows: 1 / 2
gives 0