marjanbaz
marjanbaz

Reputation: 1042

How to reset color of previously clicked button?

I have this code in my onClickListener (I have something else, but it's not relevant to this question:

final OnClickListener clickListener = new OnClickListener() {


            public void onClick(View v) {

                Button button = (Button) v;
                button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));

}
}

I set the buttons color to green (with some opacity) when I press it. How to clear that color, reset it to my default button color (not Android default, MY default set color), and have only currently pressed button to be green?

EDIT:

Here's my whole onClickListener code:

final OnClickListener clickListener = new OnClickListener() {

            private Button buttonClicked;

            public void onClick(View v) {

                Button button = (Button) v;
                button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));

                if (buttonClicked == null) {
                    // first button is clicked
                    buttonClicked = button;
                } // only do stuff if buttons are in different layouts
          else if (button.getParent () != buttonClicked.getParent()) {
                    // second button is clicked
                    if (buttonClicked.getTag().equals(button.getTag())) {
                        Toast.makeText(Spojnice.this, "Tacno", Toast.LENGTH_SHORT).show();
                        button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.setEnabled(false);
                        button.setEnabled(false);
                    } else {
                        Toast.makeText(Spojnice.this, "Netacno", Toast.LENGTH_SHORT).show();
                        //buttonClicked.setEnabled(false);
                        //buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFCC99));
                        //button.getBackground().clearColorFilter();
                    }
                    buttonClicked = null;
                }       
            }
     };

Upvotes: 1

Views: 1641

Answers (2)

Diego Torres Milano
Diego Torres Milano

Reputation: 69368

Use:

button.getBackground().setColorFilter(null);

to remove the color filter.

Upvotes: 1

Nickolai Astashonok
Nickolai Astashonok

Reputation: 2888

Use smth like for button background:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/drawable1" /> -- when button is pressed
     <item android:drawable="@drawable/drawable2" /> -- button isn't pressed
 </selector>

Upvotes: 1

Related Questions