javaandroidkotlinmaterial-designandroid-popupwindow

Android PopupWindow dismiss interrupts the ripple effect half way through


I have a PopupWindow which uses a custom layout with a single Button.

The Button has android:background="?attr/selectableItemBackground" set to have a ripple effect. The ripple effect works nicely if there's no dismiss called.

When I call dismiss on the popupwindow from the OnClickListener, the ripple effect gets interrupted half way through the dismissing. You can see the circle from the ripple but instead of it goes to the edges as the PopupWindow fades away, the ripple gets interrupted half way and you can see a circle while the popup fades away.

It's as if the dismiss is stopping the animation mid way. Anyone know why and how I can fix it?


Solution

  • I had this issue and struggled 2 weeks to fix it. Yes dismiss Popup Window is culprit, action dismiss() stops/clears all the pending animations.

    Here is what I did to show the ripple effect on clicking the item in my custom PopupWindow:

        // Define click listener for the ViewHolder's View.
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewCompat.postOnAnimationDelayed(v, new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Log.i("test", "Position: "+position);
                        // Perform Action
                    }
                }, 150); // Delay the onClick action
    
            }
        });
    

    This will delay the onClick action by 150 microseconds, which will allow ripple effect to take place before dismiss() is triggered the Popup Window.

    You must adjust onClick delay/duration and the dismiss fade animation duration to make it work as expected.