Mohamed Naguib
Mohamed Naguib

Reputation: 1730

Android button 2 clicks

How to change the android button after 2 clicks ? the first time to change button i will use this code

{
    public void onClick(View v) {
        b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
    }
}

I want to change the button view again after pressing it one more time how can i do that ?

Upvotes: 1

Views: 335

Answers (3)

George Karanikas
George Karanikas

Reputation: 1244

Well, one way is to keep a counter.

numberOfClicks = 0;
...
public void onClick(View v) {
  ...
  if(numberOfClicks==0)
    b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
  else if(numberofClicks==1)
    b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
  ...
  numberofClicks++;
}

Upvotes: 2

Kuffs
Kuffs

Reputation: 35661

private int clickCount =0;

public void onClick(View v) {

    if (clickCount==0) {
        b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
    } else {
        // do something else
    }
    clickCount++;
}

Upvotes: 2

waqaslam
waqaslam

Reputation: 68177

Perhaps do it like this:

int count = 0;

public void onClick(View v) {
    count++;

   if(count == 2){
      count = 0;
      b.setBackgroundDrawable(getResources()
                      .getDrawable(R.drawable.menubuttonpressed));
   }
}

This will set the background after every 2nd click on your button (view).

Upvotes: 2

Related Questions