Jennifer
Jennifer

Reputation: 1852

How to make a single menu button do on and off?

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;

Upvotes: 0

Views: 89

Answers (1)

Tenaciousd93
Tenaciousd93

Reputation: 3488

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;
}

Upvotes: 1

Related Questions