Question

When I hide or show the ActionBar with getActionBar().hide() or getActionBar().show() the action bar (dis)appears in a smooth animation.

Is there a way to get notified once this animation is complete?

Implementing a OnLayoutChangeListener doesn't appear to be a good option as onLayoutChange() is sometimes also called during the animation and there is no way to determine which call is the last and final call.

Was it helpful?

Solution

Is there a way to get notified once this animation is complete?

Yes, but you'll have to use reflection. ActionBarImpl uses an Animator to monitor the show and hide animation, so you can attach your own AnimatorListener to this Field and receive the callback.

Here's an example:

private void monitorActionBarAnimation() {
    final ActionBar actionBar = getActionBar();
    try {
        // Get the Animator used internally
        final Class<?> actionBarImpl = actionBar.getClass();
        final Field currentAnimField = actionBarImpl.getDeclaredField("mCurrentShowAnim");

        // Monitor the animation
        final Animator currentAnim = (Animator) currentAnimField.get(actionBar);
        currentAnim.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                // Do something
            }

        });
    } catch (final Exception ignored) {
        // Nothing to do
    }
}

Just make sure you use it after you call ActionBar.show or ActionBar.hide because it isn't initialized before those calls. Also, it's released in AnimatorListener.onAnimationEnd so you'll need to call it each time.

OTHER TIPS

You can use ActionBar.getHeight() to find out the height of the action bar, and when your layout height grows by that many pixels you know the animation has finished.

Alternatively, when ActionBar.getHeight() returns 0, you know the action bar is hidden.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top