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.

thenosic
  • 1,463
  • 2
  • 18
  • 22

4 Answers4

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"
user
  • 86,916
  • 18
  • 197
  • 190
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)

Never Quit
  • 2,072
  • 1
  • 21
  • 44
David Olsson
  • 8,085
  • 3
  • 30
  • 38
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>
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0

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

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Pradeep Sodhi
  • 2,135
  • 1
  • 19
  • 19