-4

I have 4 bottons and I set a image as a background on each button. What I want to do is that the button should turn into gray when not selected and turn into its original image color when selected. how can I do this??

     <Button
            android:id="@+id/btn_settingTab"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/setting_icon"
            android:layout_centerHorizontal="true"/>
심희수
  • 123
  • 1
  • 2
  • 5

1 Answers1

1

Why not you use Toggle Button?

Create a xml name like_button.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/likeactivepressed" />
    <item android:state_pressed="true" android:drawable="@drawable/likeinitialpressed"/>
    <item android:state_checked="true" android:drawable="@drawable/likeon"/>
    <item android:drawable="@drawable/likeinitial"/>
</selector>

Then define it in your button like this

 android:background="@drawable/like_button"
ZeroOne
  • 8,996
  • 4
  • 27
  • 45