Last active
August 29, 2015 13:55
-
-
Save mrleolink/8780750 to your computer and use it in GitHub Desktop.
A really useful gist which helps to create pressed state for Android's Button/ImageButton without using additional drawbles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.leolink.android.gist; | |
| public class MultiStateDrawable extends LayerDrawable { | |
| // The color filter to apply when the button is pressed | |
| protected ColorFilter _pressedFilter = new LightingColorFilter(Color.GRAY, 1); | |
| public MultiStateDrawable(Drawable d) { | |
| super(new Drawable[] { d }); | |
| } | |
| @Override | |
| protected boolean onStateChange(int[] states) { | |
| boolean enabled = false; | |
| boolean pressed = false; | |
| for (int state : states) { | |
| if (state == android.R.attr.state_enabled) | |
| enabled = true; | |
| else if (state == android.R.attr.state_pressed) | |
| pressed = true; | |
| } | |
| mutate(); | |
| if (enabled) { | |
| if (pressed) { | |
| setColorFilter(_pressedFilter); | |
| } else { | |
| setColorFilter(null); | |
| } | |
| } else { | |
| setColorFilter(_pressedFilter); | |
| } | |
| invalidateSelf(); | |
| return super.onStateChange(states); | |
| } | |
| @Override | |
| public boolean isStateful() { | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment