I have a wizard in my app where users can go back and forward between activities using “Back” and “Next” buttons. These buttons are the same for every activity in the wizard and are defined in their own xml layout which gets included in the the layout for each of the activities. They have a state list as their background which usually works, except once in awhile this happens:
Expected:

Actual:

Button Definition – add_nav_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlAddNav"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
orientation="horizontal">
<Button
android:id="@+id/bAddBack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/b_add_nav_button"
android:text="@string/bBack">
</Button>
<Button
android:id="@+id/bAddNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/b_add_nav_button"
android:text="@string/bNext">
</Button>
</LinearLayout>
</RelativeLayout>
Include Example – some_activity_layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlAddMain"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- OTHER STUFF -->
<include
android:id="@+id/NavButtons"
layout="@layout/add_nav_buttons"
android:layout_marginTop="-50dp"
android:layout_alignParentBottom="true">
</include>
</RelativeLayout>
StateList Definition – b_add_nav_button.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<gradient
android:startColor="#C7C7C7"
android:endColor="#8B8B8B"
android:angle="270">
</gradient>
<stroke
android:width="1dp"
android:color="#8B8B8B">
</stroke>
<padding
android:left="10dp"
android:right="10dp"
android:top="10dp"
android:bottom="10dp">
</padding>
</shape>
</item>
<item android:state_pressed="true" >
<shape>
<solid
android:color="#FFA319" />
<padding
android:left="10dp"
android:right="10dp"
android:top="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#4F4FB2"
android:endColor="#24248F"
android:angle="270">
</gradient>
<stroke
android:width="1dp"
android:color="#24248F">
</stroke>
<padding
android:left="10dp"
android:right="10dp"
android:top="10dp"
android:bottom="10dp">
</padding>
</shape>
</item>
</selector>
I am completely baffled, any help would be much appreciated. Just to re-iterate, most of the time the expected behaviour happens, just once in awhile the background fails to draw.
I should also add that the buttons still functionally work like they’re supposed to.
Edit
I added some debug statements and managed to reproduce. Apparently Android sometimes interprets b_add_bav_button.xml as a ColorDrawable instead of a StateListDrawable. I tried pulling the shapes out of the state list into their own xml files, no dice. I’m still equally baffled.
Android thought I was using a ColorDrawable instead of a StateListDrawable which resulted in the behaviour mentioned above. I still have no idea why, but it had something to do with screen orientation (it was reproducible by starting activity, popping activity, rotating screen, and starting activity again).
My work around was to pull the <shape>’s out of the xml selector and instead build a
StateListDrawable programmatically like so:
There is a lot of weirdness going on, but I seem to have it working.