android - Adding a color filter to a Drawable changes all Buttons using the same Drawable -


i have screen multiple buttons use same background drawable. have reusable code use in various projects add ontouch listener adds gray color filter while button being touched. works fine, in case buttons tinted when of them pressed.

i see explanation in http://developer.android.com/guide/topics/graphics/2d-graphics.html:

note: each unique resource in project can maintain 1 state, no matter how many different objects may instantiate it. example, if instantiate 2 drawable objects same image resource, change property (such alpha) 1 of drawables, affect other.

the suggested solution use tweenanimation, not seem work color filters.

i saw android: cloning drawable in order make statelistdrawable filters suggests using drawable.getconstantstate().newdrawable(). not seem make difference. i'm guessing long same physical image file used, drawables affected change other drawable using same resource.

what solution there, other creating second background image show pressed state? nice have simple programmatic solution can add code , use in every project.

example should work you:

drawable buttonbackground = context.getresources().getdrawable(r.drawable.bg); buttonbackground = buttonbackground.mutate();  //set filter here 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -