Android ImageButton Gets Gray Background

I have a button with a good background selector. OK. instead of the text in the button, I want an image. I tried just changing it to ImageButton with the src attribute. when I do this, it looks like the gray saddle is overlapping behind my selector, behind the src image.

when I return to the normal button, the problem disappears. what i want is just my background selector as well as the src image (instead of the button text).

any ideas?

+3
source share
4 answers

ImageButton must 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>
+8
source

What @kdumitru said, but with the right 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>
+2
source

I would say that you cannot correctly refer to an image in R.drawable, maybe you need to place the image in a drawing? ImageView works.

0
source

Can you post a screenshot of what you see? It's hard to say what you're saying, but it looks like your src image may not have the proper alpha channel. Is this PNG transparent? Make sure your src image is a properly saved, transparent PNG image.

0
source

Source: https://habr.com/ru/post/1795893/


All Articles