1

I have an image button and I want to change the color of a button when pressed. First I show it with a transparent as a default state color as below. But it doesn't show highlighted when pressed. So I decided to use other color to highlight when pressed using state drawable. But how to keep background color through styles wise. Means by default it should be transparent and when pressed it has to show other color.

present code:

<ImageButton 
        android:id="@+id/imgbutton"
            android:layout_width="20dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher"
            android:background="@android:color/transparent"
        />

Need to be coded:

<ImageButton 
        android:id="@+id/imgbutton"
            android:layout_width="20dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher"
            android:background="@drawable/btnselector"
        />

So what should be the btnselector.xml here?

Spike
  • 147
  • 3
  • 17

1 Answers1

1

put it inside res/color

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

<item android:state_pressed="true" android:color="@color/green_text_color"/> <!-- pressed -->
<item android:state_focused="true" android:color="@color/green_text_color"/> <!-- focused -->
<item android:color="@android:color/white"/> <!-- default -->

</selector>

create a new shape, inside res/drawable

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle"> 
     <solid android:color="@color/yourxmlfile" /> 
</shape>

and put it as background

or a res/drawable selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
      <item android:drawable="@drawable/green_botton_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/green_botton_highlited"
          android:state_focused="true" />
    <item android:drawable="@drawable/green_botton" />

</selector>
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • It throws an error as item tag requires a drawable attribute. That is why I posted this question. Because in every example, I found only drawable but not this way. – Spike May 27 '13 at 07:17
  • yes sorry, I should mentioned that I put it inside the res/color folder. Do you want drawable instenad of color? – Blackbelt May 27 '13 at 07:18
  • If I put this res/color folder. How to refer that file with android:background? – Spike May 27 '13 at 07:22
  • with @color/nameOfYourFileXml – Blackbelt May 27 '13 at 07:28
  • Did it work at your side? Please clarify. Because it throws same exception that item tag requires drawable attribute. – Spike May 27 '13 at 07:30
  • yes, it works. Do you want a selector for the drawable folder? – Blackbelt May 27 '13 at 07:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30671/discussion-between-ark-and-blackbelt) – Spike May 27 '13 at 07:38
  • I used [this](http://stackoverflow.com/a/14105253/1594658) answer and it did the trick. Thanks for your time. Can you please look at [this](http://stackoverflow.com/questions/16767259/preparing-custom-action-bar)? – Spike May 27 '13 at 08:40