3

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

I want set background = @null and this works fine.

But, when i press this imagebutton i can not see the color that indicates that I am pressing the button.

if i remove background = @null when i pressing the button, change the color for the user know you are pressing.

I read that you can make a xml with 3 images specifying when the button is pressed, when this normal and when it has focus. But I think exists an easier way to do.

3
  • maybe you said with 3 images... is a good idea.
    – idiottiger
    Mar 14, 2012 at 8:47
  • then I have to create 2 or 3 images for each button??
    – thenosic
    Mar 14, 2012 at 9:58
  • using selector to define different time, it also use 2 or 3 images for each button.
    – idiottiger
    Mar 14, 2012 at 10:01

4 Answers 4

13

Following the above answers, use a selector drawable(for the background, instead of null) that has the default state set to a 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 the ImageButton:

background = "@drawable/selector_with_transparency"
1
  • 1
    This works great, thank you! It behaves just like ?android:attr/selectableItemBackground in ICS. Aug 6, 2012 at 13:10
2

You removed the background because you don't like it. A background which uses a state list, depending on how you touch it, you dont use 3 images yourself. In other words, you don't show that the button is pressed and you need to do that. Then use 3 images. Just use the image you have and create two other indicating 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 really is incredibly easy, just put that xml file in your drawable folder. (res/drawable)

0
1

By default android uses R.drawable.button_default (button.default.xml) as it background until the user explicitly sets background. The best way is to create selector. The 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>
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.