Problem: My Calculations are off when I compared the results on my app to the bmi website. THank you so much in advance, also my math is really bad so I apologize in advance.
Here are the results that my app is giving me:
http://www.tiikoni.com/tis/view/?id=8bd04d4
Here are the results based of the BMI Website NIH
http://www.tiikoni.com/tis/view/?id=86d4458
BMIFRAG.java
public class BmiFrag extends Fragment implements View.OnClickListener
{
Button BmiButton;
public static EditText heightFT;
public static EditText heightIn;
public static EditText weightIn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_bmi, container,
false);
BmiButton = (Button) myView.findViewById(R.id.CalculateBmi);
BmiButton.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.CalculateBmi:
weightIn = (EditText)
getActivity().findViewById(R.id.ETtweight);
heightIn = (EditText)
getActivity().findViewById(R.id.ETHeightIn);
heightFT = (EditText)
getActivity().findViewById(R.id.ETHeightFT);
final TextView tv4 = (TextView)
getActivity().findViewById(R.id.TFDisplayBmi);
String getWeightIN = weightIn.getText().toString();
String getHeightIN = heightIn.getText().toString();
String getHeightFT = heightFT.getText().toString();
if (TextUtils.isEmpty(getWeightIN)) {
weightIn.setError("Please enter your weight");
weightIn.requestFocus();
return;
}
else if (TextUtils.isEmpty(getHeightIN)) {
heightIn.setError("Please enter your height in Inches");
heightIn.requestFocus();
return;
}
else if (TextUtils.isEmpty(getHeightFT)) {
heightFT.setError("Please enter your height in Feet");
heightFT.requestFocus();
return;
}
else {
float weight = Float.parseFloat(getWeightIN );
float heightIN = Float.parseFloat(getHeightIN) ;
float heightFT = Float.parseFloat(getHeightFT) ;
float bmiValue = calculateBMI(weight,heightIN,heightFT);
String bmiInterpretation = interpretBMI(bmiValue);
tv4.setText(String.valueOf(bmiValue + "-" +
bmiInterpretation));
}
break;
}
}
private float calculateBMI(float weight, float heightIN, float v) {
float bmi= (float) (weight/ (heightIN*v)*4.88);
float total= Math.round(bmi);
return total;
}
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onDetach() {
super.onDetach();
}
You don't use the same formula. Try this:
float bmi = weight * 703f / (float)Math.pow(heightIN+12f*v,2);
The formula above can be found here.
I hope it helps.
Related
I am facing issue while subtracting my product, I am using three buttons to get the value of the selected one and then multiply these value for add more product or subtract . But when i click on subtract button it will minus the whole amount. So please help me in this. Below mention is my code. If you want some more info then please ask me.
public class ShowDetailActivity extends AppCompatActivity {
private TextView addToCardBtn;
private TextView titleTxt, feeTxt, descriptionTxt, numberOrderTxt;
private ImageView plusBtn, minusBtn, picFood, priceBtn, mediumPriceBtn, largePriceBtn;
private ProductsDomain object;
private int numberOrder = 1;
private ManagementCart managementCart;
private LinearLayout cheese_ll;
private LinearLayout scale_ll;
private int itemPrice;
private CheckBox cheeseBoxyes;
private int price;
private int checkvalue;
private int uncheckValue;
private boolean cheeseBoolean = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_detail);
managementCart = new ManagementCart(this);
initView();
getBundle();
//Button Code
price=object.getPrice();
priceBtn.setOnClickListener(this::onClick);
mediumPriceBtn.setOnClickListener(this::onClick);
largePriceBtn.setOnClickListener(this::onClick);
//check box code for extra cheese
}
private void onClick(View view) {
int id=view.getId();
switch (id){
case R.id.smallPriceBtn:
price=object.getPrice();
itemPrice=object.getPrice();
feeTxt.setText(Integer.toString(price*numberOrder));
break;
case R.id.mediumPriceBtn:
price = object.getMediumPrice();
feeTxt.setText(Integer.toString(price*numberOrder));
itemPrice=object.getMediumPrice();
break;
case R.id.largePriceBtn:
price = object.getLargePrice();
feeTxt.setText(Integer.toString(price*numberOrder));
itemPrice=object.getLargePrice();
break;
}
}
private void getBundle() {
object = (ProductsDomain) getIntent().getSerializableExtra("object");
if (object.getWithCheese() == 1)//get cheese
{
cheese_ll.setVisibility(View.VISIBLE);
scale_ll.setVisibility(View.GONE);
} else {
cheese_ll.setVisibility(View.GONE);
scale_ll.setVisibility(View.VISIBLE);
}
Glide.with(this).load("http://192.168.100.215/pizzaVill/Images/" + object.getImage()).into(picFood);
titleTxt.setText(object.getName());
descriptionTxt.setText(object.getDescription());
numberOrderTxt.setText(Integer.toString(numberOrder));
feeTxt.setText(Integer.toString(object.getPrice()));
plusBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
numberOrder = numberOrder + 1;
numberOrderTxt.setText(Integer.toString(numberOrder));
feeTxt.setText(Integer.toString(numberOrder * price));
//Code if there is no size required
}
});
minusBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (numberOrder > 1) {
numberOrder = numberOrder - 1;
}
numberOrderTxt.setText(Integer.toString(numberOrder));
feeTxt.setText(Integer.toString(price - itemPrice));
}
});
addToCardBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
object.setNumberInCard(numberOrder);
object.setFinalPrice(price);
managementCart.insertFood(object);
}
});
}
private void initView() {
addToCardBtn = findViewById(R.id.addToCardBtn);
titleTxt = findViewById(R.id.titleTxt);
feeTxt = findViewById(R.id.priceTxt);
descriptionTxt = findViewById(R.id.descriptionTxt);
numberOrderTxt = findViewById(R.id.numberOrderTxt);
plusBtn = findViewById(R.id.plusBtn);
minusBtn = findViewById(R.id.minusBtn);
picFood = findViewById(R.id.foodPic);
priceBtn = findViewById(R.id.smallPriceBtn);
mediumPriceBtn = findViewById(R.id.mediumPriceBtn);
largePriceBtn = findViewById(R.id.largePriceBtn);
cheese_ll = findViewById(R.id.cheese_ll);
scale_ll = findViewById(R.id.scale_ll);
cheeseBoxyes = findViewById(R.id.cheeseBoxyes);
}
im having a trouble while i call acce variable of this class
public class Slider extends AppCompatActivity {
...
public double[] acce={3.00,2.00,1.00};
public double[] gyro={3.00,2.00,1.00};
#Override
protected void onCreate(Bundle savedInstanceState) {
...
accelerometer = new Accelerometer(this);
gyroscope = new Gyroscope(this);
accelerometer.setListner(new Accelerometer.Listner() {
#Override
public double[] onTranslation(float ax, float ay, float az) {
acce = setAccValue(ax,ay,az);
return acce;
}
...
}
from this class
public void AntremanSayfasi(Context context){
...
Slider slider = new Slider();
AntremanaBasla.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
aa = (float) slider.acce[0];
bb = (float) slider.acce[1];
cc = (float) slider.acce[2];
accx.setText(String.valueOf(aa));
accy.setText(String.valueOf(bb));
accz.setText(String.valueOf(cc));
}
});
}
the problem is... i can get the determined values but cannot get the updated values of sensor change ?
sorry for rooky question, any help will be appritiated
UPDATE
this is the main Class
public class Slider extends AppCompatActivity {
private ViewPager AktifHareket;
private LinearLayout mDotLayout;
private String asd;
private int asdf;
private Antreman antreman;
TextView DenemeSayiCek;
int position;
View view;
int count;
View viewFix;
Context context;
//Sensors-------------------------------------------------------------------------------------------
private Accelerometer accelerometer;
private Gyroscope gyroscope;
public double[] acc={3.00,2.00,1.00};
public double[] gyr={3.00,2.00,1.00};
public double[] acce={5.00,5.00,5.00};
public double[] gyro={3.00,2.00,1.00};
//Sensors---------------------------------------------------------------------------------------
//Main------------------------------------------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Antreman antreman = new Antreman();
context = this;
setContentView(R.layout.activity_slider);
Login login = new Login();
AktifHareket = (ViewPager) findViewById(R.id.slideViewPager);
mDotLayout = (LinearLayout) findViewById(R.id.dotsLayout);
SliderAdapter sliderAdapter = new SliderAdapter(this);
AktifHareket.setAdapter((sliderAdapter));
addDotsIndicator();
//Saydir Buton------------------------------------------------------------------------------
TextView Deneme = (TextView) findViewById(R.id.deneme);
TextView Deneme4 = (TextView) findViewById(R.id.deneme4);
TextView Deneme3 = (TextView) findViewById(R.id.deneme3);
TextView Deneme2 = (TextView) findViewById(R.id.deneme2);
Button Saydir = (Button) findViewById(R.id.SaydirSlide);
Button Saydir2 = (Button) findViewById(R.id.Saydir2Slide);
Saydir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
position = AktifHareket.getCurrentItem();
view = AktifHareket.getChildAt(position);
count = Integer.valueOf(AktifHareket.indexOfChild(view));
viewFix = AktifHareket.getChildAt(count);
DenemeSayiCek = (TextView) viewFix.findViewById(R.id.TekrarSayisiSlide);
asd = (String) DenemeSayiCek.getText().toString();
asdf = Integer.valueOf(asd);
asdf++;
DenemeSayiCek.setText(String.valueOf(asdf));
Deneme.setText(Arrays.toString(acce));
Deneme2.setText(String.valueOf(position));
Deneme3.setText(Arrays.toString(acce));
Deneme4.setText(String.valueOf(SliderAdapter.POSITION_NONE));
}
});
//Saydir Buton------------------------------------------------------------------------------
//ImageButton-------------------------------------------------------------------------------
ImageView logoImage = (ImageView) findViewById(R.id.logo);
logoImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
antreman.AntremanSayfasi(context);
}
});
antreman.AntremanSayfasi(context);//Açılışta Sayfayı açsın diye
//ImageButton-------------------------------------------------------------------------------
//Sensors-----------------------------------------------------------------------------------
accelerometer = new Accelerometer(this);
gyroscope = new Gyroscope(this);
accelerometer.setListner(new Accelerometer.Listner() {
#Override
public double[] onTranslation(float ax, float ay, float az) {
acce = setAccValue(ax,ay,az);
Antreman.SensorValues.accValues=acce;
return acce;
}
});
gyroscope.setListner(new Gyroscope.Listner() {
#Override
public void onRotation(float gx, float gy, float gz) {
setGyroValue(gx,gy,gz);
}
});
//Sensors-----------------------------------------------------------------------------------
}
//Sensors---------------------------------------------------------------------------------------
protected void onResume(){
super.onResume();
accelerometer.register();
gyroscope.register();
}
protected void onPouse(){
super.onPause();
accelerometer.unregister();
gyroscope.unregister();
}
public double[] setAccValue(double ac, double bc, double cc){
this.acc[0] = ac;
this.acc[1] = bc;
this.acc[2] = cc;
return acc;
}
public double[] setGyroValue(float qq, float wq, float eq){
this.gyr[0] = qq;
this.gyr[1] = wq;
this.gyr[2] = eq;
return gyr;
}
public void getAccValue(double ac, double bc, double cc){
ac = (float) acce[0];
bc = (float) acce[1];
cc = (float) acce[2];
}
public void getGyroValue(float qq, float wq, float eq){
qq = (float) gyro[0];
wq = (float) gyro[1];
eq = (float) gyro[2];
}
//Sensors---------------------------------------------------------------------------------------
public void addDotsIndicator(){
TextView[] mDots = new TextView[3];
for(int i = 0; i < mDots.length; i++){
mDots[i] = new TextView(this);
mDots[i].setText(Html.fromHtml("•"));
mDots[i].setTextSize(35);
mDots[i].setTextColor(getResources().getColor(R.color.colorPrimaryDark));
mDotLayout.addView(mDots[i]);
}
}
//----------------------------------------------------------------------------------------------
}
ant this is the class i need to initialize those values from the main class
public class Antreman {
private Accelerometer accelerometer;
private Gyroscope gyroscope;
public double aa;
public float bb;
public float cc;
public float qq;
public float ww;
public float ee;
//Pop up----------------------------------------------------------------------------------------
public void AntremanSayfasi(Context context){
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
final View popup_durum = li.inflate(R.layout.popup_durum,null);
Button AntremanaBasla = popup_durum.findViewById(R.id.basla);
Button Cikis = popup_durum.findViewById(R.id.cikis);
dialogBuilder.setView(popup_durum);
AlertDialog dialog = dialogBuilder.create();
dialog.show();
TextView gyrox = (TextView) popup_durum.findViewById(R.id.gyrox);
AntremanaBasla.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
aa = SensorValues.accValues[0];
gyrox.setText(String.valueOf(aa));
}
});
Cikis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
static class SensorValues{
public static double[] accValues = {1.00};
}
}
you can use the concept of common class in this case and I believe it will solve your problem quite easily. Follow to steps below.
Make a public class and declare an array of double inside it which you need to access in another class
Public class Common{
public static List<double> acce;
}
and in your activity where the sensors give updated value use a temp list
and assign it to Common.acce;
Common.acce = tempList;
Now, when you need to access acce just use Common.acce, and the reference of tempList is in the Common.acce and when the data in tempList will be updated by your sensor the list in common will be also updated, so ultimately you will get the updated value.
Feel free to ask if something is unclear. And Kindly mark it as the correct answer if it helps you so that in future this answer can help other needy. Happy Coding 😀.
I'm trying to do a division (double/double) and the result is Infinity. Can someone help me and explain why that's happening?
I searched some forums, but I didn't get the answer.
I have something like that:
public class CalculoSapIsolada extends AppCompatActivity
{
double areaAcoX;
double numeroBarrasX;
double areaBarraX;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculo_sap_isolada);
//Here is where i get the value of areaBarraX
if(SapataIsolada.getAço()==50)
{
final Spinner staticSpinner = (Spinner) findViewById(R.id.spinner_diametro_aço_x);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(this, R.array.diametro_aço_CA_50, android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setAdapter(staticAdapter);
staticSpinner.setPrompt("Selecione o diâmetro da armadura");
staticSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
Object item = adapterView.getItemAtPosition(i);
if (i == 0) {
diametroAçoX = 0.5;
areaBarraX = 0.196;
}
if (i == 1) {
diametroAçoX = 0.63;
areaBarraX = 0.31;
}
if (i == 2) {
diametroAçoX = 0.8;
areaBarraX = 0.5;
}
if (i == 3) {
diametroAçoX = 1;
areaBarraX = 0.785;
}
if (i == 4) {
diametroAçoX = 1.25;
areaBarraX = 1.22;
}
if (i == 5) {
diametroAçoX = 1.6;
areaBarraX = 2.01;
}
if (i == 6) {
diametroAçoX = 2;
areaBarraX = 3.14;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
/* the value of areaAcoX is calculated previously and during the testrun it receives areaAcoX=11.36016 and the value of areaBarraX=0.5 */
numeroBarrasX=areaAcoX/areaBarraX;
Button calcular =(Button)findViewById(R.id.calcular_resultado);
calcular.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//in altura.setText, areaAcoX==11.36016
TextView altura = (TextView)findViewById(R.id.alturaTotal);
altura.setText(""+areaAcoX);
//In ladoLtopo.setText, areaBarraX==0.5
TextView ladoLtopo = (TextView)findViewById(R.id.ladoL_topo);
ladoLtopo.setText(""+areaBarraX);
//In numBarrasX.setText, numeroBarrasX==Infinity
TextView numBarrasX = (TextView)findViewById(R.id.numeroBarras_x);
numBarrasX.setText(""+numeroBarrasX);
});
}
}
I have some similar operations in the code that is not shown here and I didn't get the infinity error in their value.
Dividing by 0.0 of type double yields infinity. Perhaps there's a divide by 0.0 at this line before areaBarraX has been initialized:
numeroBarrasX=areaAcoX/areaBarraX;
I've one problem. I've created spinner, and everything app runs without crashing, but it won't change value of int price, neither setText doesn't work
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button calculateBtn;
EditText userEcts;
TextView ectsPrice;
TextView summary;
Spinner courses;
String cors;
int price = 200;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userEcts = (EditText) findViewById(R.id.ects_input);
summary = (TextView) findViewById(R.id.text_summary);
courses = (Spinner) findViewById(R.id.course_spinner);
calculateBtn = (Button) findViewById(R.id.calculate);
calculateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//When the button is clicked, call the calculate method.
calculate();
}
});
final String[] courseArray = new String[4];
{
courseArray[0] = "Preddiplomski studij Menadžment";
courseArray[1] = "Preddiplomski studij Promet";
courseArray[2] = "Preddiplomski Upravni studij";
courseArray[3] = "Specijalistički studij Menadžment";
}
ArrayAdapter courseAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, courseArray);
courseAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
courses.setAdapter(courseAdapter);
courses.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Get item from Spinner and store in string conductorSize........
cors = parent.getItemAtPosition(pos).toString();
if (cors.equals(0)) {
ectsPrice.setText("200");
price = 200;
} else if (cors.equals(1)) {
ectsPrice.setText("250");
price = 250;
} else if (cors.equals(2)) {
ectsPrice.setText("300");
price = 300;
} else if (cors.equals(3)) {
ectsPrice.setText("350");
price = 350;
} else {
//Do nothing
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
}
public void calculate() {
//gets entered text from the EditText,and convert to integers.
if (!TextUtils.isEmpty(userEcts.getText().toString())) {
Double ects = Double.parseDouble(userEcts.getText().toString());
//do the calculation
Double calculatedPrice = ects * price;
//set the value to the TextView, to display on screen.
summary.setText("Ukupno za platit: " + Double.toString(calculatedPrice) + "kn");
} else {
Toast.makeText(this, "Niste unijeli broj bodova", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onClick(View v) {
calculate();
}
}
So, on selecting item on position 0, I want to setText to specific text, and change price. Can anyone help?
Use the position number to check the condition.
String[] courseArray = new String[4];
{
courseArray[0] = "Preddiplomski studij Menadžment";
courseArray[1] = "Preddiplomski studij Promet";
courseArray[2] = "Preddiplomski Upravni studij";
courseArray[3] = "Specijalistički studij Menadžment";
}
ArrayAdapter courseAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, courseArray);
courseAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
courses.setAdapter(courseAdapter);
courses.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Get item from Spinner and store in string conductorSize........
cors = parent.getItemAtPosition(pos).toString();
if (cors.equals(courseArray[0])) {
ectsPrice.setText("200");
price = 200;
} else if (cors.equals(courseArray[1])) {
ectsPrice.setText("250");
price = 250;
} else if (cors.equals(courseArray[2])) {
ectsPrice.setText("300");
price = 300;
} else if (cors.equals(courseArray[3])) {
ectsPrice.setText("350");
price = 350;
} else {
//Do nothing
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
}
Use a switch statment :
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
switch(pos){
case 0:
ectsPrice.setText("200");
price = 200;
break;
case 1:
ectsPrice.setText("250");
price = 250;
break;
case 2:
ectsPrice.setText("300");
price = 300;
break;
case 3:
ectsPrice.setText("350");
price = 350;
break;
}
}
For some reason my input Validation is not working. Every time I put calculate it crashes "app" when it should have an error saying that I need to input height/weight. When I do input the numbers it does calculate. Thanks for the help :). I'm new to android studio .
here is my calculation java file
public class BmiFrag extends Fragment implements View.OnClickListener {
Button BmiButton;
private double weight1=0;
private double height1=0;
public static EditText heightIn;
public static EditText weightIn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_bmi, container, false);
BmiButton = (Button) myView.findViewById(R.id.CalculateBmi);
BmiButton.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.CalculateBmi:
weightIn = (EditText)
getActivity().findViewById(R.id.ETtweight);
heightIn = (EditText) getActivity().findViewById(R.id.ETHeight);
final TextView tv4 = (TextView)
getActivity().findViewById(R.id.TFDisplayBmi);
String str1 = weightIn.getText().toString();
String str2 = heightIn.getText().toString();
float weight = Float.parseFloat(str1);
float height = Float.parseFloat(str2) ;
float bmiValue = calculateBMI(weight, height);
String bmiInterpretation = interpretBMI(bmiValue);
tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));
if (TextUtils.isEmpty(str1)) {
weightIn.setError("Please enter your weight");
weightIn.requestFocus();
return;
}
else if (TextUtils.isEmpty(str2)) {
heightIn.setError("Please enter your height");
heightIn.requestFocus();
return;
}
break;
}
}
private float calculateBMI(float weight, float height) {
float bmi= (float) (weight/ (height*height)*4.88);
float total= Math.round(bmi);
return total;
}
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onDestroy() {
}
#Override
public void onDetach() {
super.onDetach();
}
}
Try to change the code like the following,
if (TextUtils.isEmpty(str1)) {
weightIn.setError("Please enter your weight");
weightIn.requestFocus();
return;
}
else if (TextUtils.isEmpty(str2)) {
heightIn.setError("Please enter your height");
heightIn.requestFocus();
return;
}else{
float weight = Float.parseFloat(str1);
float height = Float.parseFloat(str2) ;
float bmiValue = calculateBMI(weight, height);
String bmiInterpretation = interpretBMI(bmiValue);
tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));
}
if (TextUtils.isEmpty(str1)) {
weightIn.setError("Please enter your weight");
weightIn.requestFocus();
return;
}
if (TextUtils.isEmpty(str2)) {
heightIn.setError("Please enter your height");
heightIn.requestFocus();
return;
}
firstly, sort your indentation and variable names out. Never name a variable str1, str2: always meaningful names. Indentation should always be consistent. This will help out but fixing in the future for readability and speed.
You're doing input validation after you actually input and assign things through
calculateBMI() method
That part of your code reads: Lets take text from text fields, interpret the BMI and then see if the textfields are empty