I have an ImageButton
, where the source image is loaded dynamically from the network. My goal is to set different color filters on the image according to the button's state (i.e state_pressed, state_focused...). The problem is that I cant use Selector
because my image is loaded dynamically and is not a drawable resource.
Does any one can give me a clue how can I recognize the state using code and imitate the selector's work?
Asked
Active
Viewed 999 times
0

ET.
- 1,899
- 2
- 18
- 28
1 Answers
1
You can set the states programmatically as outlined by kcoppock in this SO post
Resources r = getResources();
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, r.getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused}, r.getDrawable(R.drawable.focused));
states.addState(new int[] {}, r.getDrawable(R.drawable.normal));
imageButton.setImageDrawable(states);
-
1And as you have dynamic images, instead if `r.getDrawable(...)` use `Drawable.createFromPath(pathName)`. – Ridcully Nov 18 '12 at 18:57
-
@antwe: It seems like a good start, but how do I set the color filter? Calling 'setColorFilter' on the same drawable seem to have no effect. – ET. Nov 18 '12 at 22:14
-
@Ridcully: I forgot to thank you for your comment, it completed the answer. so - thank you :) – ET. Nov 19 '12 at 18:32