Silesia
Silesia

Reputation: 67

ImageButton - toggle color when pressed

How can i toggle the color of an ImageButton when user presses the ImageButton?

I want the ImageButton to toggle between red (power off) and green (power) when pressed.

Code:

    ImageButton star, power;
Intent i;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    star = (ImageButton)findViewById(R.id.ibStar);
    star.setOnClickListener(this);

    power = (ImageButton)findViewById(R.id.ibPower);
    power.setOnTouchListener(this);


}

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN ) {
        power.setImageResource(R.drawable.power);

    }
     else if (event.getAction() == MotionEvent.ACTION_UP ) {
        power.setImageResource(R.drawable.power_off);

    }

    return true;
}

Upvotes: 1

Views: 373

Answers (3)

prolink007
prolink007

Reputation: 34594

Try using the OnClickListener instead of the OnTouch. There are a few ways you can handle this situation.

One way is to have a boolean that toggles between true and false signifying if the power is on or off. And then check that value when the ImageButton is pressed and change the color of the ImageButton accordingly.

/* The boolean below is
 * declared in the 
 * class and not in a method. 
 * Whatever default value you need, i assumed false. */
private boolean isPowerOn = false;

power = (ImageButton)findViewById(R.id.ibPower);
power.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
            if (isPowerOn) {
                /* set color to red */
                isPowerOn = false;
            } else {
                /* set power to green */
                isPowerOn = true;
            }
        }
    });

OR

You can check the color of the ImageButton when it is pressed and then change the color accordingly. This way seems a little funky. Use the boolean method.

Upvotes: 1

Andrzej Gis
Andrzej Gis

Reputation: 14316

I think this is what you need.

ImageButton star, power;
Intent i;
boolean isOn=false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    star = (ImageButton)findViewById(R.id.ibStar);
    star.setOnClickListener(this);

    power = (ImageButton)findViewById(R.id.ibPower);
    power.setOnTouchListener(this);


}

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN ) {
        isOn=!isOn; // change its state to the oposite one

        if(isOn)
            power.setImageResource(R.drawable.power);
        else
            power.setImageResource(R.drawable.power_off);
    }

    return true;
}

Upvotes: 2

CSmith
CSmith

Reputation: 13458

If you want to have the button press event show a different color (i.e. during the press event), use a selector, like shown at Button Background Selector.

If you want to have the button color toggle between red and green after an onClick event, then use an onClickListener() (pseudo-code):

onClick(ImageButton ib)
{
 if (button is green)
  ib.setImageResource(red)
 else
  ib.setImageResource(green)
}

Upvotes: 0

Related Questions