0

I wanted to make a plain button in the menu act as a on/off button like a toggle button. But I'm not sure how I can make a single button act like a switch?

switch(item.getItemId()){
    case R.id.switcher:

        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(Color.parseColor("#80FFFFFF"));

                 //I want to change the color of the background by clicking once
                  //and set the background color back to normal. How will I achieve this ?
    return true;
Jennifer
  • 1,822
  • 2
  • 20
  • 45

1 Answers1

1

Try this:

@Override
public void onClick(View v) {
    if ((v.getId() == R.id.my_button){
        buttonOnClick(v);
    }
}

private void buttonOnClick(View v) {
    switch (v.getId()) {
    case R.id.my_button: {
    if (v.isSelected()) {
        // is selected, deselect!
        v.setSelected(false);
        //do your staff here
    } else {
        // is not selected, select!
        v.setSelected(true);
        //do your staff here
    }
    break;
    }
    default:
        break;
}
Tenaciousd93
  • 3,438
  • 4
  • 33
  • 56
  • Thank you. But how can I use if(isSelected()) with MenuItem's item ? – Jennifer Apr 14 '14 at 08:56
  • 1
    Mmmh, I see now that menuItem doesn't have `isSelected` method. I think you can build your custom menu inflating xml layout in actionbarsherlock, otherwise, try checking [Vikas](http://stackoverflow.com/questions/5440601/android-how-to-enable-disable-option-menu-item-on-button-click) answers: it looks like would help you. – Tenaciousd93 Apr 14 '14 at 09:07