Luis Tartareli
Luis Tartareli

Reputation: 1

How to put 2 events in a single button on android studio Java

`So i've been having a problem with my code, im currently trying to put 2 events on a single button. But it only allows one event at a time

    public void event1(View view) {
    boolean retorno = true;

    String c1 = et1.getText().toString();
    String c2 = et2.getText().toString();
    String c3 = et3.getText().toString();

    if (c1.isEmpty()) {
        retorno = false;
    }
    if (c2.isEmpty()) {
        retorno = false;
    }
    if (c3.isEmpty()) {
        retorno = false;
    }
}

public void event2(View view) {
    double num1, num2, num3;
    double MlGrama, LKG;
    String select;

    num1 = parseDouble(et1.getText().toString());
    num2 = parseDouble(et2.getText().toString());
    num3 = parseDouble(et3.getText().toString());
    select = sp1.getSelectedItem().toString();

    if (select.equals("Ml/Grama")) {
        MlGrama = (num1 / num2) * num3;
        DecimalFormat df = new DecimalFormat(".00");

        tv3.setText("O resultado é: " + df.format(MlGrama));
    } else {
        if (select.equals("Litro/Kg")) {
            LKG = ((num1 * 1000) * num2) * num3;
            DecimalFormat df = new DecimalFormat(".00");

            tv3.setText("O resultado é: " + df.format(LKG));
        }
    }
}


    android:onClick="event1, event2"

I tried using setOnClickListener, but it only makes one of the two actions. I also tried using the 2 actions in one void, but it also does not work.

Upvotes: 0

Views: 33

Answers (1)

Royi
Royi

Reputation: 306

If you want to call 2 different events in one action why don't you put them one after another like this:

public void onButtonClicked(View view) {
    event1(view);
    event2(view);
}

android:onClick="onButtonClicked"

Upvotes: 1

Related Questions