Change alpha on a statelist drawable button?

Kevin

Well-Known Member
Licensed User
Longtime User
I've seen similar solutions but not quite what I'm looking for...

I have About 50+/- buttons in a layout. Each button is set to StatelistDrawable and has an 'enabled' and 'pressed' image assigned to it. Essentially the 'pressed' image is a slightly darker copy of the original 'enabled' image (that I did in a graphics editor). Of course this means that each button has two different images, and multiply that times 50 and it adds overhead to the app.

What I would like to do is assign the same bitmap (png) to the enabled and pressed states and programmatically adjust the alpha on the pressed image in order to make the button darker (or dimmed) when pressed.

Can this be done using reflection or any other trick? Just trying to reduce memory requirements and speed things up a bit if possible.
 

Kevin

Well-Known Member
Licensed User
Longtime User
I thought about that (and may still do it) but since some buttons are shaped funny (in graphical appearance.... they are square or rectangular of course) I have Z-order set for some buttons. In those cases, I'd have to make sure the z-order doesn't cause the panel to partially overlap the wrong button.

If I went that route and worked out the z-order issues, I assume this would probably cut down on some memory overhead and layout load times? This would take me quite a while to do and I want the efforts to be worthwhile.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Well I gave it a test-run and I don't think it's going to work without a whole lot of tweaking. Unfortunately the 50 or so buttons is on one of three similar layouts. I was able to tweak things enough to make the panel look perfect on one of the them but when I tried it on the others, there was a whole lot more to be done and since these layouts also change dynamically through code depending on the device resolution, this would be way more work than I want to take on right now.

Any other ideas? I assume it isn't possible to change the alpha on a StatelistDrawable button? Between this and some other streamlining ideas I had, I was hoping to shave several hundred KB off of the APK, but the other ideas I was kicking around turned out to be more work than expected as well.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…