Using default button drawable in selector for Android application

1.4k Views Asked by At

Currently I am using this selector:

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

I want my to set default drawable for my button in case it is not pressed and not focused. How should I modify my selector to do this?

2

There are 2 best solutions below

2
On

Try this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:drawable="@drawable/button" 
        android:state_focused="false" 
        android:state_pressed="false" 
        android:state_selected="false"/>
    <!-- Pressed -->
    <item android:drawable="@drawable/button_pressed" 
        android:state_focused="false" 
        android:state_pressed="true" 
        android:state_selected="false"/>
</selector>
0
On

Define different colors for various states as follows (please use your own hex codes for colors) in a res/values/colors.xml file

<color name="green_pressed">#ff00f000</color>
<color name="green_focused">#ff00f700</color>
<color name="green_default">#ff00ff00</color>

Declare various drawables for different states

button_focused_green.xml

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

    <solid android:color="@color/green_focused" />
    <!-- optional, remove if you don't want round border -->
    <corners android:radius="4dp" />
</shape>

button_pressed_green.xml

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

    <solid android:color="@color/green_pressed" />
    <!-- optional, remove if you don't want round border -->
    <corners android:radius="4dp" />
</shape>

button_default_green.xml

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

    <solid android:color="@color/green_default" />
    <!-- optional, remove if you don't want round border -->
    <corners android:radius="4dp" />
</shape>

Declare a StateListDrawable in xml file which will be applied to the Button

button_green.xml

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

    <item android:drawable="@drawable/button_focused_green" android:state_focused="true"/>
    <item android:drawable="@drawable/button_pressed_green" android:state_pressed="true"/>        
    <item android:drawable="@drawable/button_default_green"/>

</selector>

Set the background attribute of Button in your layout xml

<Button 
    ...
    android:background="@drawable/button_green"
.../>

Hope this helps.