4

i have a Button with a nice background selector. fine. in stead of text in the button, i want an image. i have tried just changing it to an ImageButton with a src attribute. when i do this, it looks like a gray background is overlaid behind my selector, behind the src image.

when i change back to a regular Button, the problem goes away. what i want is just my background selector, plus the src image (in stead of the button text).

any ideas?

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134

4 Answers4

11

ImageButton should have android:background set.

<ImageButton 
    android:id="@+id/ibArrow"
    android:layout_width="35px"
    android:layout_height="50px"
    android:src="@drawable/arrow"
    android:background="@drawable/backgroundstate" />   

And backgroundstate:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@color/transparent" />
        <item android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:drawable="@color/transparent" />
   </selector>
Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
kdumitru
  • 126
  • 1
  • 4
2

What @kdumitru said, but with the correct XML stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@android:color/transparent" />
    <item android:state_pressed="true" android:drawable="@android:color/transparent" />
    <item android:drawable="@android:color/transparent" />
</selector>
Pierre
  • 1,252
  • 1
  • 14
  • 24
0

I would say you might not be referencing the image correctly in R.drawable maybe you need to place image in drawable? ImageView does work.

0

Can you post a screenshot of what you're seeing? Hard to tell exactly what you're saying, but it sounds like your src image may not have a proper alpha channel. Is it a PNG with transparency? Make sure your src image is a properly saved, transparent PNG image.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • here's an example of one of the images i tried: http://cdn1.iconfinder.com/data/icons/humano2/128x128/actions/gtk-media-forward-ltr.png. to work around this, i used a regular button, removed the text, reference the image via android:drawableTop, then squashed the button by manually setting the height. the method shows the image and background correctly. based on this, my assumption is that the image is correct. – Jeffrey Blattman Mar 03 '11 at 01:55
  • Can you take a screenshot of what the issue looks like? Your transparency seems fine. – Kevin Coppock Mar 03 '11 at 04:57