0

I have a button that changes the image when it clicked.
I've done that with this code.

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

    <item android:drawable="@drawable/ic_fullscreen_gmap" android:state_selected="false"/>
    <item android:drawable="@drawable/ic_fullscreen_exit_gmap"/>
</selector>  

Is it possible to change the color of the default button but Together with the Image?
I want to change it to something.
The default button color is something like gray and I want to change that.

Charles Galvez
  • 1,100
  • 5
  • 19
  • 41

2 Answers2

1

To change both the image and the background color of your Button, you could use an ImageButton and define separate selectors for the android:src and android:background attributes.

For example:

button_src_selector.xml:

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

    <item android:state_pressed="true"
          android:drawable="@drawable/ic_fullscreen_exit_gmap"/>
</selector>

button_bg_selector.xml:

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

    <item android:state_pressed="true"
          android:drawable="@color/color2"/>
</selector>

And the ImageButton:

<ImageButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button_src_selector"
    android:background="@drawable/button_bg_selector"/>
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
0

Following code will help you to change default button background:

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_bg_normal"></item>
</selector>
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
Anjali
  • 1
  • 1
  • 13
  • 20
  • I guess you misunderstood my question. What I want is, The color changed at the same time with the image when the button is clicked. – Charles Galvez Sep 15 '16 at 06:42