java - Android BMI calculation in CM and KG -
hye, want create bmi simple android application. found code code calculate in lbs , inch. but, program need create in kg , cm. try recode back. but, still cant find solution.
this code.
package com.example.bmicalculator; import android.app.activity; import android.os.bundle; import android.widget.textview; import android.widget.edittext; import android.widget.button; import android.view.view; public class bmicalculatoractivity extends activity { private edittext txtheight; private edittext txtweight; private textview txtresult; private button btncalculate; private double bmi = 0; private double valueheight = 0; private double valueweight = 0; private string resulttext; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); initcontrols(); /*textview tv = new textview(this); tv.settext("bmi calculator"); setcontentview(tv);*/ } private void initcontrols() { txtheight = (edittext)findviewbyid(r.id.txtheight); txtweight = (edittext)findviewbyid(r.id.txtweight); txtresult = (textview)findviewbyid(r.id.txtresult); btncalculate = (button)findviewbyid(r.id.btncalculate); //btnreset = (button)findviewbyid(r.id.btnreset); btncalculate.setonclicklistener(new button.onclicklistener() { public void onclick (view v){ calculate(); }}); //btnreset.setonclicklistener(new button.onclicklistener() { public void onclick (view v){ reset(); }}); } private void calculate() { valueheight =double.parsedouble(txtheight.gettext().tostring()); valueweight =double.parsedouble(txtweight.gettext().tostring()); bmi = (valueweight / (valueheight * valueheight)); //txttipamount.settext(double.tostring(bmi)); if (bmi >= 30) { /* obese */ resulttext = "your bmi of " + double.tostring(bmi) + " obese."; txtresult.settext(resulttext); } else if (bmi >= 25) { resulttext = "your bmi of " + double.tostring(bmi) + " overweight."; txtresult.settext(resulttext); } else if (bmi >= 18.5) { resulttext = "your bmi of " + double.tostring(bmi) + " ideal."; txtresult.settext(resulttext); } else { resulttext = "your bmi of " + double.tostring(bmi) + " underweight."; txtresult.settext(resulttext); } } private void reset() { txtresult.settext("your bmi unknown."); txtheight.settext("0"); txtweight.settext("0"); } }
thanks in advance.
to bmi (body mass index) in metric, need divide weight in kilograms height in meters^2. if asking height in cm, divide 100 before calculation. example:
private void calculate() { valueheight =double.parsedouble(txtheight.gettext().tostring()); valueweight =double.parsedouble(txtweight.gettext().tostring()); double valueheightmeters; valueheightmeters = valueheight / 100; // converting meters. bmi = (valueweight / (valueheightmeters * valueheightmeters)); //txttipamount.settext(double.tostring(bmi)); if (bmi >= 30) { /* obese */ resulttext = "your bmi of " + double.tostring(bmi) + " obese."; txtresult.settext(resulttext); } else if (bmi >= 25) { resulttext = "your bmi of " + double.tostring(bmi) + " overweight."; txtresult.settext(resulttext); } else if (bmi >= 18.5) { resulttext = "your bmi of " + double.tostring(bmi) + " ideal."; txtresult.settext(resulttext); } else { resulttext = "your bmi of " + double.tostring(bmi) + " underweight."; txtresult.settext(resulttext); } }
hope helps!
Comments
Post a Comment