Android: button with custom layout and day/night themes
<p>I'm trying to create a Button with a custom layout and 2 themes (day and night).</p>

<p>The button layout has a background image (drawable) and a text (string).</p>

<p>The themes must differ in drawable (I have two PNGs) and text color.</p>

<p>My approach is as follows:</p>

<ol>
<li><p>In the button layout set the background drawable as a selector:</p>

<p>
    
</p></li>
<li><p>this is the drawable/btn_basket_calls.xml</p>

<p></p>

<pre><code>&lt;item
    android:drawable="@drawable/btn_basket_calls_pressed"
    android:state_focused="true"
    android:state_pressed="true"
    /&gt;

&lt;item
    android:drawable="@drawable/btn_basket_calls_pressed"
    android:state_focused="false"
    android:state_pressed="true"
    /&gt;

&lt;item
    android:drawable="@drawable/btn_basket_calls_selected"
    android:state_focused="true"
    /&gt;

&lt;item
    android:drawable="@drawable/btn_basket_calls_disabled"
    android:state_enabled="false"
    /&gt;

&lt;item
    android:drawable="?attr/btn_basket_calls_default"
    android:state_focused="false"
    android:state_pressed="false"
    /&gt;
</code></pre>

<p></p></li>
<li><p>the "default" drawable is a reference to the attribute "btn_basket_calls_default" which is defined in attrs.xml as follows:</p>

<p>

    

</p></li>
<li><p>The AppTheme has two subthemes in styles.xml:</p>

<p></p>

<p>
    @drawable/btn_basket_calls_d_default
</p>

<p>
    @drawable/btn_basket_calls_n_default
</p>

<p></p></li>
</ol>

<p>AFAIK it should work :) however the project complies fine but gives a runtime exception:</p>

<pre><code>E/AndroidRuntime( 1154): FATAL EXCEPTION: main
E/AndroidRuntime( 1154): java.lang.RuntimeException: Unable to start activity ComponentInfo{a.daynighttest/a.daynighttest.DayNightTestActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class &lt;unknown&gt;
E/AndroidRuntime( 1154):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime( 1154):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 1154):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 1154):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 1154):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1154):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 1154):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 1154):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1154):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1154):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1154):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1154):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1154): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class &lt;unknown&gt;
E/AndroidRuntime( 1154):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
E/AndroidRuntime( 1154):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime( 1154):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
E/AndroidRuntime( 1154):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
E/AndroidRuntime( 1154):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
E/AndroidRuntime( 1154):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 1154):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime( 1154):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
E/AndroidRuntime( 1154):    at android.app.Activity.setContentView(Activity.java:1657)
E/AndroidRuntime( 1154):    at a.daynighttest.DayNightTestActivity.onCreate(DayNightTestActivity.java:13)
E/AndroidRuntime( 1154):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 1154):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime( 1154):    ... 11 more
E/AndroidRuntime( 1154): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 1154):    at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 1154):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
E/AndroidRuntime( 1154):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
E/AndroidRuntime( 1154):    ... 22 more
E/AndroidRuntime( 1154): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_basket_calls.xml from drawable resource ID #0x7f020000
E/AndroidRuntime( 1154):    at android.content.res.Resources.loadDrawable(Resources.java:1697)
E/AndroidRuntime( 1154):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime( 1154):    at android.view.View.&lt;init&gt;(View.java:1951)
E/AndroidRuntime( 1154):    at android.view.View.&lt;init&gt;(View.java:1899)
E/AndroidRuntime( 1154):    at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:286)
E/AndroidRuntime( 1154):    at android.widget.LinearLayout.&lt;init&gt;(LinearLayout.java:120)
E/AndroidRuntime( 1154):    ... 25 more
E/AndroidRuntime( 1154): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #31: &lt;item&gt; tag requires a 'drawable' attribute or child tag defining a drawable
E/AndroidRuntime( 1154):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:167)
E/AndroidRuntime( 1154):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
E/AndroidRuntime( 1154):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
E/AndroidRuntime( 1154):    at android.content.res.Resources.loadDrawable(Resources.java:1694)
E/AndroidRuntime( 1154):    ... 30 more
</code></pre>

<p>See attached complete Eclipse project for details.</p>

<p>Thanks! :)</p>
