Is there any way to set the color and background image for a button? I want a green button with a transparent .png but it seems you can only have one or the other.
4 Answers
(Don't) Use an ImageView
Set your set your green button background image as the background, and your transparent png as the src. You can apply a selector to the background just the same as you would to a button also...
EDIT: Actually there is an ImageButton already made for this, you should be using that instead. Slipped my mind.

- 46,603
- 18
- 125
- 156
-
1Thanks, I was using `Button` instead of `ImageButton`. It works now. I'm now using `ImageButton` with `android:background` for the color and `android:src` for the image. – gatzkerob May 06 '12 at 23:54
Try This piece of code it works for me :)
<ImageButton
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
android:src="@drawable/settingbtn"
android:background="@drawable/red_color"
/>

- 137
- 15
Draw separate images with the transparency and save the image. To show different images for the instances of the image being pressed,focused etc.. use the following method.
Use the image button with the background as the xml shown below:
`<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/imagewhenpressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/imagewhenfocused" /> <!-- focused -->
<item android:drawable="@drawable/defaultimage" /> <!-- default -->
</selector>`
Note: Here the three images -imagewhenpressed.png,imagewhenfocused.png & defaultimage.png and this xml should be placed in the drawable directory. The ImageButton background should be assigned to the filename
of this xml like android:background="@drawable/xmlfilename"

- 1,021
- 1
- 12
- 17
Use a ImageButton
instead.
You can set a background like normal and in the src
property you can select an image.
You can also mess with the scaleType
property if the image gets too small or too big.

- 3,744
- 2
- 31
- 34