0

I have a button on my screen and I'm using my own custom XML file to define how this button looks.

The problem I come across is that when this button is pressed, the effect that usually shows the button in a pressed state does not occur.

my XML file

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

        <corners
            android:bottomLeftRadius="7dp"
            android:bottomRightRadius="7dp"
            android:radius="7dp"
            android:topLeftRadius="7dp"
            android:topRightRadius="7dp"
             />
        <solid
            android:color="@color/PaleGoldenrod"/>


    </shape>

I'm not very familiar with this and google did not yield results because my question might be vague or I phrased it poorly.

Space Ghost
  • 765
  • 2
  • 13
  • 26

1 Answers1

2

You need to define a pressed state using a color state list XML resource. See http://developer.android.com/guide/topics/resources/color-list-resource.html.

You need to create the selector resource as the button's background, and then you define a different shape XML for each state.

Very simple version:

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

button_shape_normal.xml (this is your shape drawable)

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

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:radius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"
         />
    <solid
        android:color="@color/PaleGoldenrod"/>
</shape>

button_shape_pressed.xml (this is your pressed state)

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

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:radius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"
         />
    <solid
        android:color="@color/PressedColor"/>
</shape>

You may want to change the shape of the button on your pressed state, or whatever. The point is you can completely customize your pressed state, but you have to create a new xml file for it.

jacobhyphenated
  • 2,105
  • 2
  • 17
  • 27