Hashey100
Hashey100

Reputation: 984

How can i get a button to perform 2 operations

How can i get one button to perform 2 operations.

Upvotes: 0

Views: 310

Answers (2)

Bill Gary
Bill Gary

Reputation: 3005

something like this, set up a global variable i and set it to 0. then when you click the button the first time it will do the first code then set i to 1. the second time it will do the second code and set i back to 0, so you can loop through those 2 every other push of the button.

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:
        if (i == 0) {
            //display 1 code
            i = 1;
        }
        if (i == 1) {
            // display 4 code
            i = 0;
        }

        break;
    }

}

Upvotes: 3

czupe
czupe

Reputation: 4919

You should get the id:

  public void onClick(View v) {
      if( button1.getId() == ((Button)v).getId() ){
          //1st button action
      }
      else if( button2.getId() == ((Button)v).getId() ){
          //2nd button action
      }
  }

Upvotes: 0

Related Questions