4

My question has to do with applying an image overlay to all button pressed events. So, for example, in Android 4.0 and higher with the holo setup, when you press any button, it has the light blue overlay on the button. In the earlier version of android, it is orange. I know how to setup the button to change the image for pressed, focused, and default in the XML with the selector and item tags, but that would require me to make another image of my button but with a light blue overlay which means i know have 2 pictures for every button. My app is going to have a lot of buttons, and i'd like to keep the file size of the app as small as possible.

So the point of the question is, is there a way to make just 1 picture (the light blue 25% opacity image) apply as an overlay to all button presses while also keeping the original background of the button I had set stay there?

matthias krull
  • 4,389
  • 3
  • 34
  • 54
Razgriz231
  • 105
  • 2
  • 10

1 Answers1

2

Load in the image. Then draw the alpha color ontop of it.

Then use the modified image to set the button Image.

Bitmap img = BitmapFactory.decodeFile(pathName); // or decodeResource  etc....

Canvas canvas = new Canvas(img);
Paint alphaPaint = new Paint();

// Color to apply.
alphaPaint.setColor(Color.BLUE);
alphaPaint.setAlpha(20);

// Draw rectangle over your image using the alpha colored paint.
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), alphaPaint);
myButton.setImageBitmap(img );
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • You could easily write this into a method to pass in your `Bitmap` and desired `Color`, and get out the modified coloured bitmap. for your button – IAmGroot Dec 03 '12 at 16:34
  • I keep having issues with it giving me an immutable bitmap null point issue. I looked that up and found a method that can convert an immutable bitmap to a mutable one. Is this something that needs to be done or am i missing something? (my picture is a standard .png) – Razgriz231 Dec 03 '12 at 21:51
  • @user1873141 If you are recieving immutable bitmap, then make a mutable copy of it yes. I personally have never had that problem, so unsure why it occurs. http://stackoverflow.com/a/13119762/940834 – IAmGroot Dec 04 '12 at 09:43
  • I got it to work, it was tricky at first because it had no errors, but I didn't see a change. Then I found out an alpha value of 20 is almost impossible to see. Once I made it 80, I could notice it. Only final question I have is, is this something I should have trigger in the onClick event? – Razgriz231 Dec 04 '12 at 19:07
  • I came up with another way to do it that is a little easier. You can make your standard picture the "android:background". Then for the overlay part, just create a xml file that has the standard selector and item stuff for doing button states, but for that xml, only have the "pressed" and "focused" states in it. Then set the drawables for those states (inside the xml you just made) to 1 picture that is an image of your color at the desired opacity. Then back to the imagebutton, set your "android:src" to that xml you made, and then it works like a charm. – Razgriz231 Dec 04 '12 at 21:01
  • @Razgriz231 can you tell me in detail how did you do it? WIth sample code of the selector – Gaurav Oct 02 '13 at 11:00