2

Long time stackoverflow reader, first time with a question. The problem I am having seems silly, but I can't find any information that explains it.

I am new to Android programming and am working on a project with custom button backgrounds. I am using state list drawables for the different buttons that look like this:

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


<item>
    <bitmap android:src="@drawable/button_pressed_background" android:gravity="center"/>
    android:state_pressed="true"
</item>

<item>
    <bitmap android:src="@drawable/button_focused_background" android:gravity="center"/>
    android:state_focused="true"
</item>

 <item>
    <bitmap android:src="@drawable/button_standard_background" android:gravity="center"/>
    android:state_pressed="false"
    android:state_focused="false"
</item>


</selector>

In the xml where the buttons are declared, I simply add the line android:background="@drawable/button_drawable" where button_drawable.xml is the state list drawable.

Seems simple enough, but in all cases, the buttons display with the background listed in the first <item> section of the state list drawable, no matter their state. In fact, in the example above, I could change the first <item> section to include android:state_pressed="false" instead, and the button_pressed_background still displays! It is as if all button states are true and false simultaneously.

I am developing using emulators, but see this behavior both with the AVD emulator and with Android x86 running on Oracle VirtualBox. Any idea why this would occur?

RyanBiggs
  • 23
  • 3

2 Answers2

0

Define a selector with the below code and try it as background:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/background_sel"/>
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/background_sel"/>
    <item android:state_focused="true" android:drawable="@drawable/background_sel"/>
    <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/background_normal"/>
</selector>
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

Itz not android:src, its android:drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
    android:drawable="@drawable/btn_new_default" />    
<item android:state_pressed="true" 
    android:drawable="@drawable/btn_new_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
    android:drawable="@drawable/btn_new_default" />
<item android:state_enabled="true"
    android:drawable="@drawable/btn_new_default" />

For more action get the original file from

drive letter:\android-sdk-windows_new\platforms\android-8\data\res\drawable\btn_default.xml

Deepzz
  • 4,573
  • 1
  • 28
  • 52
Hardik4560
  • 3,202
  • 1
  • 20
  • 31