Search code examples
androidpopuppopupwindow

PopupWindow overlaps Navigation Drawer


Currently my popup is overlapping other views. setElevation(0) changes nothing. setOverlapAnchor(false) and setAttachedInDecor(true) also don't help much. Below is the code I have used. I need the popup to be located under navigation drawer

  private fun showPopup(anchorView: View) {
        PopupWindow(
            LayoutInflater.from(activity).inflate(
                R.layout.popup_layout,
                null
            ),
            100,
            100,
            false
        )
       .apply {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    view?.elevation = 0f
                    contentView.elevation = 0f
                    elevation = 0f
                }
                isTouchable = false
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                    isAttachedInDecor = true
                }

                PopupWindowCompat.setOverlapAnchor(this, false)
                PopupWindowCompat.showAsDropDown(this, anchorView, 0, 0, Gravity.NO_GRAVITY)
            }
    }

enter image description here


Solution

  • PopupWindow is a window. Your navigation drawer is located on another window with its own view hierarchy.

    Its like this:

    -- activity
    ---- window1
    ------ viewhierarchy
    --------NavigationDrawer
    ---- window2
    ------ popup

    What you want is not possible using PopupWindow.

    One possible work-around is to hide & show the popup when navigation is opened and closed. Here's the callback:

    https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html#ondrawerstatechanged

    Or you may add a view as popup by yourself and take care of the positioning and gravity.

    Last but not least, check out these libraries as they may have what you want. They work with view so you may manage it the way you want.

    https://github.com/sephiroth74/android-target-tooltip

    https://github.com/tomergoldst/tooltips