Home » Android » animation – Button states with Background as AnimationDrawable in Android

animation – Button states with Background as AnimationDrawable in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
  <item android:drawable="@drawable/frame1" android:duration="600" /> 
  <item android:drawable="@drawable/frame2" android:duration="300" /> 
  <item android:drawable="@drawable/frame3" android:duration="500" /> 
</animation-list> 

If I set the animation as button’s Background it works fine. If I try to make a simple selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
        android:state_pressed="false"
        android:drawable="@drawable/animation" />

    <item
        android:state_pressed="true"
        android:drawable="@drawable/pressed" />
  </selector>      

where normal state of the button would have animation as background and the pressed state a static image, things don’t work right.

On my main activity, on onWindowFocus I get the button background and start the animation

 @Override
  public void onWindowFocusChanged(boolean hasFocus) {
      super.onWindowFocusChanged(hasFocus);
          btn = (Button)findViewById(R.id.btnAnim);
          btnAnimation = (AnimationDrawable) btnAnim.getBackground();
          btnAnimation.start();
 }

Here appears to be the problem, because my animation won’t be taken correctly from the selector and I get the following error:

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 ...

Any idea on how to fix this ? Thanks.

How to&Answers:

You’re doing incorrect cast — your background drawable is StateListDrawable, not AnimationDrawable. I’d rather do something like:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  btn = (Button)findViewById(R.id.btnAnim);
  StateListDrawable background = (StateListDrawable) btn.getBackground();
  Drawable current = background.getCurrent();
  if (current instanceof AnimationDrawable) {
      btnAnimation = (AnimationDrawable) current;
      btnAnimation.start();
  }
}

Answer:

My answer is a bit late, I know, but I faced the same issue. I checked a lot of solutions, but found only one.
I have tried to start the animation in onWindowFocusChanged(), start the animation in aseparate thread, but it doesn’t help.

I solved this issue using setVisible (boolean visible, boolean restart)

So you can try this:

    private Button ImgBtn;
    private AnimationDrawable btnAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.button1);
        StateListDrawable background = (StateListDrawable) btn.getBackground();
        btnAnimation = (AnimationDrawable) background.getCurrent();
        btnAnimation.setVisible(true, true); // it works even in onCreate()        
    }

Hope this will help somebody 🙂