Emzor
Emzor

Reputation: 1368

How can I use a single button click to achieve multiple actions in android eclipse?

I'm new to Java and Android development with Eclipse, so please pardon my "stupid" question (and my moniker).

I'm creating an app that takes in user inputs- weight and height, calculates the user's BMI (Body-Mass Index), double bmi = weight/(height * height); and displays the output in a textView. I've pretty much achieved that, but I'm stuck with the issues of how to:

Firstly, use a single button click to display the BMI value on one textView1;

Secondly, display some text on a different textView2 depending on the range the user's BMI value falls into, using the if statement below:

if(bmi >= 0 && bmi < 22.01)
        {
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.print(". That is too low, you are underweight.\nEat regularly and put on some weight.");
        }else if(bmi >= 22.01 && bmi < 25.01){
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.print(". That is normal, maintain your healthy lifestyle.");
        }else if(bmi >= 25.01 && bmi < 30.01){
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.print(". That is high, you are overweight!\nGet on a diet and exercise regularly.");
        }else if(bmi >= 30.01 && bmi < 40.01){
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.print(". That is very high, you are OBESE!\nGet on a diet and exercise as often as you can.");
        }else{
            System.out.printf("Your BMI is " + "%1.2f", bmi);
            System.out.println("That is way too high, you are SEVERELY OBESE!\nGet on a diet asap and exercise everyday!");
        }

I've searched this site and others but couldn't sufficient help in this regard. Thanks a bunch, in anticipation of your kind assistance!

Upvotes: 2

Views: 309

Answers (2)

Didi78
Didi78

Reputation: 247

you should use 2 editText's for the input, and 2 TextView to display the BMI and to display the "Secendly":

in your main activity xml:

 <EditText 
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/Weight"
    android:hint="@string/Weight"
    android:inputType="number"/>

 <EditText 
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/Height"
    android:hint="@string/Height"
    android:inputType="number"/>

    <Button
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_below="@+id/Height"
    android:id="@+id/button"
    android:text="BMI" />

    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/BMI"
    android:layout_marginBottom="10dp" />

    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/BMItext"
    android:layout_marginBottom="10dp" />

and in your code:

public class MainActivity extends Activity {

private EditText Weight, Height;
private TextView BMI, BMItext;
private Button button;

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

    BMItext = (TextView) findViewById(R.id.BMItext);        
    BMI = (TextView) findViewById(R.id.BMI);        
    Weight = (EditText) findViewById(R.id.Weight);
    Height = (EditText) findViewById(R.id.Height);  
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           Double Weightnum= Weight.getText().toString();
           Double WeightHeight = Weight.getText().toString()*Height.getText().toString();
            Double calculator = Weightnum/WeightHeight;
            BMI.setText("Your BMI is:" + calculator);
if(calculator >= 0 && calculator < 22.01)
    {
BMItext.settext("That is too low, you are underweight.\nEat regularly and put on some weight.");
                }else if(calculator >= 22.01 && calculator < 25.01){
        BMItext.settext("That is normal, maintain your healthy lifestyle.");
    }else if(calculator >= 25.01 && calculator < 30.01){
        BMItext.settext("That is high, you are overweight!\nGet on a diet and exercise regularly.");
    }else if(bmi >= 30.01 && bmi < 40.01){
        BMItext.settext("That is very high, you are OBESE!\nGet on a diet and exercise as often as you can.");
    }else{
        BMItext.settext("That is way too high, you are SEVERELY OBESE!\nGet on a diet asap and exercise everyday!");
    }

        }
    });

    }
 }

i hope i didnt miss anything, i created this code just here just now so.. :)

Upvotes: 0

Gallaron
Gallaron

Reputation: 31

How do you detect the click? With a onClickListener or with a onClick tag in the XML file? Either way you will get a method in your activity, which will get executed when your button is clicked. Just put every code that is supposed to get executed in this method.

Upvotes: 1

Related Questions