androideclipsebuttonroundingpressed

Making a round button look pressed in eclipse


I'm trying to make a button look pressed when you click it. But when i try to implement that code into my xml file the rounded button feature i had beforehand doest work. Im linking the xml file to the android:background command on the button.

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/light_orange" android:state_pressed="true"/>
    <item android:drawable="@color/orange"/>
<shape
android:shape="rectangle" android:padding="10dp">

<solid android:color="#FFFF00"/> 
    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
  android:topLeftRadius="10dp"
  android:topRightRadius="10dp"/>
  </shape>

</selector> 

Solution

  • You need to have three xmls in total. One with your 'normal' rounded shape, one with your rounded shape with a different color or however you want to indicate that it is pressed. And last a selector like the one that you have in your example, where you reference the other two drawables, according to the state...

    roughly something like that:

    1- button_not_pressed.xml

    <solid android:color="#FFFFFF00"/> 
        <corners
         android:bottomRightRadius="10dp"
         android:bottomLeftRadius="10dp"
      android:topLeftRadius="10dp"
      android:topRightRadius="10dp"/>
      </shape>
    

    2- button_pressed

    <solid android:color="#AADDDD00"/> 
        <corners
         android:bottomRightRadius="10dp"
         android:bottomLeftRadius="10dp"
      android:topLeftRadius="10dp"
      android:topRightRadius="10dp"/>
      </shape>
    

    3- stateful_button.xml

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

    You can then simply give your button the drawable stateful_button as background and it will change the color, when you press it...