Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android button to show and hide the same popup

Could anyone please tell me that how is it possible to show and hide the same popup window using a single button.

suggestions welcome.


Popup means that when i clicked on a menu button it showing a popup window which contains the listview and when i clicked on the menu button again and touches anywhere on the screen,the popwindow should disappear.

like image 634
raman Avatar asked Oct 29 '25 09:10

raman


1 Answers

I use the bellow code to do that. In your case the menuButton is your single button and content is your listview. The anchor can be the menuButton itself or another View.

public static void setupMenuButton(View menuButton, View content, final View anchor) {
    PopupWindow popup;

    menuButton.setOnClickListener(v -> {
        if (popup == null) {
            popup = new PopupWindow(content, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            popup.setOutsideTouchable(true);
        }

        if (popup.isShowing()) popup.dismiss();
        else popup.showAsDropDown(anchor);
    });
}
like image 116
Italo Borssatto Avatar answered Oct 31 '25 01:10

Italo Borssatto