dbar
dbar

Reputation: 1760

Android's Overflow Menu animation?

When I press the overflow menu in the action bar a dropdown menu pops down with an animation. I'm checking the sdk's res/anim folder and I would like to know which animation does this button use, so I can use it to pop down some other stuff when the user presses other buttons I have in the action bar.

Thanks!

Upvotes: 1

Views: 630

Answers (1)

HappyOrca
HappyOrca

Reputation: 89

As far as I know, on Android Api 23 the default dropdown animation is implemented by Transition. you can read PopupWindow.java's source code to know what was done and how to do.

you can find two xml files which define transitions for dropdown animtions at sdk's platforms/android-23/datas/res/transition directory.(popup_window_enter.xml, popup_window_exit.xml) unfortunately, you can't refer it via android.R.transition.popup_window_enter.xml because it's hidden. but you can copy it in to your project and using it.

below there are some codes which I use in my project.

public static void showOverflow(final RelativeLayout holder, final View anchor, final CardView cv) {
    holder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hideOverflow(holder, cv);
        }
    });
    if (cv.getVisibility() != View.VISIBLE) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                final Rect episodeCenter = getTransitionEpicenter(anchor, cv);
                Transition transition = TransitionInflater.from(holder.getContext()).inflateTransition(R.transition.popup_window_enter);
                transition.setEpicenterCallback(new Transition.EpicenterCallback() {
                    @Override
                    public Rect onGetEpicenter(Transition transition) {
                        return episodeCenter;
                    }
                });
                TransitionManager.beginDelayedTransition(cv, transition);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        cv.setVisibility(View.VISIBLE);
    }
}

private static Rect getTransitionEpicenter(View anchor, View popup) {
    final int[] anchorLocation = new int[2];
    final int[] popupLocation = new int[2];
    anchor.getLocationOnScreen(anchorLocation);
    popup.getLocationOnScreen(popupLocation);
    final Rect bounds = new Rect(0, 0, anchor.getWidth(), anchor.getHeight());
    bounds.offset(anchorLocation[0] - popupLocation[0], anchorLocation[1] - popupLocation[1]);
    return bounds;
}

Upvotes: 1

Related Questions