Charles Galvez
Charles Galvez

Reputation: 1110

Buttons With Background Color and Image

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.

Upvotes: 0

Views: 635

Answers (2)

Anjali
Anjali

Reputation: 2535

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>

Upvotes: 0

earthw0rmjim
earthw0rmjim

Reputation: 19427

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"/>

Upvotes: 1

Related Questions