Skip to content Skip to sidebar Skip to footer

Popupwindow Closing Issue By Same Button

I wanna close the PopupWindow by the same Button, But when I click on the Button again, it reopen the PopupWindow instead of closing it, and also should close the PopupWindow when

Solution 1:

As per my Way:

int x=1;
    ivmainmenu.setOnClickListener(newOnClickListener() {

         @SuppressWarnings("null")@OverridepublicvoidonClick(View v) {
           if((x%2)!=0){ 
            //Dismiss PopUp
           }else{
            //Show PopUp
           }
           x++;   
       }
    });

And also defined your popupWindow globally and make

popupWindow.setOutsideTouchable(false);popupWindow.setCanceledOnTouchOutside(false)

Update:

PopupWindowpopupWindow=null;
Boolean isShowing=false;
ivmainmenu.setOnClickListener(newOnClickListener() {

     @SuppressWarnings("null")@OverridepublicvoidonClick(View v) {

         if(isShowing)
            {
              if(popupWindow != null && popupWindow.isShowing()){
                 popupWindow.dismiss();
                 }
                isShowing=false;
            }
            else
            {
             isShowing=true;
            LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
                ViewpopupView= layoutInflater.inflate(R.layout.popupwindow, null);  
              popupWindow = newPopupWindow(popupView,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);  
                    popupWindow.showAsDropDown(ivmainmenu, 0,14);
                    popupView.setPadding(0, 0, 0, 10);    
                    popupWindow.setBackgroundDrawable(newBitmapDrawable());
                    popupWindow.setOutsideTouchable(false);
                    popupWindow.setCanceledOnTouchOutside(false)
                    popupWindow.setFocusable(true);

                TextViewtvpopupwork= (TextView)popupView.findViewById(R.id.tvpopupwork);
                TextViewtvpopupabout= (TextView)popupView.findViewById(R.id.tvpopupabout);
                TextViewtvpopupservices= (TextView)popupView.findViewById(R.id.tvpopupservices);
                TextViewtvpopupcontact= (TextView)popupView.findViewById(R.id.tvpopupcontact);


                TypefacetypeFace2=  Typeface.createFromAsset(getAssets(),"fonts/arboriaboldregular.ttf");
                tvpopupwork.setTypeface(typeFace2);
                tvpopupabout.setTypeface(typeFace2);
                tvpopupservices.setTypeface(typeFace2);
                tvpopupcontact.setTypeface(typeFace2);


                tvpopupwork.setOnClickListener(newOnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                        // TODO Auto-generated method stubIntentintent=newIntent(Home.this,Ourwork.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        popupWindow.dismiss();
                        startActivity(intent);
                        isShowing=false;
                    }
                });

                tvpopupabout.setOnClickListener(newOnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                        // TODO Auto-generated method stubIntentintent=newIntent(Home.this,Aboutus.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                         popupWindow.dismiss();
                         startActivity(intent);
                         isShowing=false;
                    }
                });

                tvpopupservices.setOnClickListener(newOnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                        // TODO Auto-generated method stubIntentintent=newIntent(Home.this,Services.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                         popupWindow.dismiss();
                         startActivity(intent);
                         isShowing=false;
                    }
                });

                tvpopupcontact.setOnClickListener(newOnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                        // TODO Auto-generated method stubIntentintent=newIntent(Home.this,Contact.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                         popupWindow.dismiss();
                         startActivity(intent);
                         isShowing=false;
                    }
                });

            }
     }
        });

give me feedback in this

Solution 2:

Change onClick() method as below:

@Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stubif(popupWindow != null && popupWindow.isShowing()){
                           popupWindow.dismiss();
                        }

                    } 

Solution 3:

If you want to close the popup with same button and on clicking anywhere outside then you will have to implement

  • The touch interceptor and dismiss the popup when action is MotionEvent.ACTION_OUTSIDE.
  • Set the popup to be focusable using setFocusable(true)

Setting focusable ensures that popup can grab outside touch events and since it will also capture the click on the menuitem or a button, It ensures that popup is not launched again if it is already showing.

To, put the above logic in code, you have to do the following.

 final MenuItem popupMenu= menu.findItem(R.id.action_open_popup);
    popupMenu.setOnMenuItemClickListener(newMenuItem.OnMenuItemClickListener() {
        @OverridepublicbooleanonMenuItemClick(MenuItem item) {
            if (window == null) {
                View contentView = getLayoutInflater(null).inflate(R.layout.popup_menu, null);
                window = newPopupWindow(contentView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
                window.setBackgroundDrawable(newBitmapDrawable(getResources(), ""));
                window.setOutsideTouchable(true);
                window.setFocusable(true);
                window.setTouchInterceptor(newView.OnTouchListener() {
                    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
                        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                            window.dismiss();
                            returntrue;
                        }
                        returnfalse;
                    }
                });
            }
            //anchor as the menuitem this is in fragment so.window.showAsDropDown(getActivity().findViewById(R.id.action_open_popup));
            returntrue;
        }
    });

Post a Comment for "Popupwindow Closing Issue By Same Button"