Search code examples
androidandroid-actionbaractionbarsherlock

How to animate home button in action bar?


I'm using ActionBarSherlock and I would like to animate the home button to tell the user to click on it. I want to use three different images to show the animation.

How can I do this?


Solution

  • Get the action bar in you onCreate:

    ActionBar actionBar = getSupportActionBar();
    

    Create and load an animation drawable which you defined in XML:

    // homeDrawable is a field on your activity
    homeDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.my_thing);
    

    Set the drawable as the icon on the action bar:

    actionBar.setIcon(homeDrawable);
    

    Post a Runnable to start the animation when the main thread is clear:

    getWindow().getDecorView().post(new Runnable() {
      @Override public void run() {
        homeDrawable.start();
      }
    });
    

    Don't forget to stop the animation at some point!