anitstudent
anitstudent

Reputation: 19

Changing color of a button in an array of buttons and return the other buttons to default

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.btnA:
            runOnUiThread(new Thread(new Runnable() {
                @Override
                public void run() {
                    btnA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.button_pressed));
                    btnB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    btnC.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    btnD.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                }
            }));
            break;
        case R.id.btnB:
            runOnUiThread(new Thread(new Runnable() {
                @Override
                public void run() {
                    btnB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.button_pressed));
                    btnA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    btnC.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                    btnD.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));                
                }
            }));
            break;
 }

So I am currently implementing my code like this it does the job fine, but I have 6 buttons so I have to do this 6 times. I've read about array of buttons and tried to implement it but could not make it work. What isn't clear to me is how do I know which button I clicked and changed it to another color while the other button that is not clicked goes back to their default color.

EDIT:
Sorry if I wasn't clear, this buttons are used for multiple choices. The buttons are already set to default on creation. Not using the switch statement would make the two buttons the same color if I click on another button after the other, they would be the same color. It's more of a display problem..

Upvotes: 1

Views: 343

Answers (3)

Maycon Prado
Maycon Prado

Reputation: 62

I guess what FredK meant was something like that:

    @Override
    public void onClick(View v) {
        runOnUiThread(new Thread(new Runnable() {
            @Override
            public void run() {
                // Reset all buttons
                btnA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                btnB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                btnC.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                btnD.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                .
                .
                .
                btnZ.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));

                // Set only the clicked button
                v.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.button_pressed));
            }
        }));
    }

UPDATE:

You can also iterate over the ViewGroup so you won't need to write down each Button manually.

    ViewGroup viewGroup = (ViewGroup) v.getParent();
    for(int i=0;i<viewGroup.getChildCount();i++){
        Object child = viewGroup.getChildAt(i);
        if(child instanceof Button){
            ((Button) child).setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
        }
    }

Upvotes: 0

Milk
Milk

Reputation: 2655

Consider moving the test into the setBackgroundColor call and keeping it all in a single new Runnable

@Override
public void onClick(View v) {
  runOnUiThread(new Thread(new Runnable() {
    @Override
    public void run() {
      btnA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), (v.getId() == R.id.btnA ? R.color.button_pressed : R.color.colorPrimary));
      btnB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), (v.getId() == R.id.btnB ? R.color.button_pressed : R.color.colorPrimary));
      ...

Upvotes: 1

FredK
FredK

Reputation: 4084

No need for a switch statement. Just set all buttons to the default color, then set the selected button to the selected color.

Upvotes: 1

Related Questions