Note that there are some explanatory texts on larger screens.

plurals
  1. POButton states with Background as AnimationDrawable in Android
    primarykey
    data
    text
    <p>I have made custom buttons in Android for a while. Things were simple, just made image resources for button states and made a selector for it. Everything went smooth and nice. Now I encountered a new situation. I have made a animation drawable and set it as a background for my button. </p> <pre><code>&lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"&gt; &lt;item android:drawable="@drawable/frame1" android:duration="600" /&gt; &lt;item android:drawable="@drawable/frame2" android:duration="300" /&gt; &lt;item android:drawable="@drawable/frame3" android:duration="500" /&gt; &lt;/animation-list&gt; </code></pre> <p>If I set the animation as button's Background it works fine. If I try to make a simple selector</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="false" android:drawable="@drawable/animation" /&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/pressed" /&gt; &lt;/selector&gt; </code></pre> <p>where normal state of the button would have animation as background and the pressed state a static image, things don't work right.</p> <p>On my main activity, on onWindowFocus I get the button background and start the animation</p> <pre><code> @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); btn = (Button)findViewById(R.id.btnAnim); btnAnimation = (AnimationDrawable) btnAnim.getBackground(); btnAnimation.start(); } </code></pre> <p>Here appears to be the problem, because my animation won't be taken correctly from the selector and I get the following error:</p> <pre><code>03-14 15:21:16.146: ERROR/AndroidRuntime(440): FATAL EXCEPTION: main 03-14 15:21:16.146: ERROR/AndroidRuntime(440): java.lang.ClassCastException: android.graphics.drawable.StateListDrawable 03-14 15:21:16.146: ERROR/AndroidRuntime(440): at com.bebenjoy.MainActivity.onWindowFocusChanged(MainActivity.java:53) 03-14 15:21:16.146: ERROR/AndroidRuntime(440): at ... </code></pre> <p>Any idea on how to fix this ? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload