Imagebutton with @null background (transparent)

I have an image button with an image source with transparency, but the background color of the image button is the color gray of the button.

I want to set background = @null and that works great.

But when I click this button, I do not see the color indicating that I am clicking the button.

If I remove background = @null when I click the button, change the color the user knows you are clicking.

I read that you can make xml with 3 images indicating when the button is clicked when it is normal and when it has focus. But I think there is an easier way to do it.

+3


source to share


4 answers


Following the above answers, use a selector highlight (for background instead of null

) that has the default state set to transparent color:

* selector_with_transparency: *

<?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/pressed" />
    <item android:drawable="@drawable/transparent" />

</selector>

      

pressed:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#0077cc" />

</shape>

      



transparent:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#00000000"/>

</shape>

      

and then for ImageButton

:

background = "@drawable/selector_with_transparency"

      

+13


source


You removed the background because you don't like it. The background that the list of states uses , depending on how you touch it, you are not using 3 images yourself. In other words, you are not showing that the button is clicked and you need to do this. Then use 3 images. Just use the image you have and create two others indicating that it is focused and pressed.

Example: main.xml

<?xml version="1.0" encoding="UTF-8"?>
 <Button 
         android:id="@+id/btn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/background_color"/>

      



background_color.xml

 <?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/button_pressed" />
        <item android:state_focused="true"
            android:drawable="@drawable/button_focused" />
        <item android:drawable="@drawable/button_normal" />
    </selector>

      

It's really incredibly simple, just put this xml file in your drawing folder. (RES / exhaust)

+2


source


By default android uses R.drawable.button_default (button.default.xml) as background, unless the user explicitly sets the background. The best way is to create a selector . A similar thing is mentioned in android sdk

button_default.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_normal_disable" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_default_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_selected" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_normal_disable_focused" />
    <item
         android:drawable="@drawable/btn_default_normal_disable" />
</selector>

      

+1


source


use for button in xml android:background="#1000"

0


source







All Articles