0

I have some code for setting the background for my buttons, but when I run the code, I don't see the effect of click in my buttons! Below, I show you my xml file for the background. Thanks!

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="5dp"
        android:right="5dp"
        android:top="5dp"
        android:bottom="5dp"
        >
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#ffe49713">
            </solid>
        </shape>
    </item>
</layer-list>
jbaums
  • 26,405
  • 5
  • 76
  • 118

2 Answers2

0

This can be achieved by creating a drawable xml file containing a list of states for the button. So for example if you create a new xml file called "button.xml" with the following code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/YOURIMAGE" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
    <item android:drawable="@drawable/YOURIMAGE" />
</selector>

To keep the background image with a darkened appearance on press, create a second xml file and call it gradient.xml with the following code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap android:src="@drawable/YOURIMAGE"/>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
        </shape>
    </item>
</layer-list>

In the xml of your button set the background to be the button xml e.g.

android:background="@drawable/button"

Hope this helps!

Changed the above code to show an image (YOURIMAGE) in the button as opposed to a block colour.

Hope above description might help you.

Let me know if you need more help from myside on same.

Vatsal Shah
  • 527
  • 3
  • 9
0

Try adding this to backgorund of your button.Change the colors according to your req

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

    <item android:state_focused="true" android:state_pressed="false" android:color="@android:color/holo_red_dark"/>
    <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff"/>
    <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff"/>
    <item android:color="@android:color/holo_red_dark"/>

</selector>
Anirudh Sharma
  • 7,928
  • 13
  • 37
  • 41