dismiss the popup window by back button
Asked Answered
S

6

13

I want to Dismiss the popup window by clicking outside of the popup window or by the back button, but when click on the back button my application exit's, instead of exiting the application I want to close the popup window.

here is my code,

ivmainmenu.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);  
        View popupView = layoutInflater.inflate(R.layout.popupwindow, null);  
        final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);  
        popupWindow.showAsDropDown(ivmainmenu, 0,14);
        popupView.setPadding(0, 0, 0, 10);
        popupWindow.showAsDropDown(ivmainmenu);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(false);

        TextView tvpopupwork = (TextView)popupView.findViewById(R.id.tvpopupwork);
        TextView tvpopupabout = (TextView)popupView.findViewById(R.id.tvpopupabout);
        TextView tvpopupservices = (TextView)popupView.findViewById(R.id.tvpopupservices);
        TextView tvpopupcontact = (TextView)popupView.findViewById(R.id.tvpopupcontact);

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

        tvpopupwork.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Home.this,Ourwork.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
            }
        });

        tvpopupabout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Home.this,Aboutus.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);  
            }
        });

        tvpopupservices.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(Home.this,Services.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
            }
        });

        tvpopupcontact.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(Home.this,Contact.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
            }
        });

        ivmainmenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                popupWindow.dismiss();
            }
        });
    }
});

Its gives me the result what I want but when I closes the menu the it does not opens again, I want to open it again so what should I do? thank you.

Savoirvivre answered 20/2, 2014 at 5:14 Comment(0)
T
29

Replace

popupWindow.setOutsideTouchable(false);

with this

popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
Tragicomedy answered 20/2, 2014 at 5:17 Comment(2)
I had to set the background to a non null drawable, at least popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));Acrylic
Also, to listen it's state use setOnDismissListenerGoatskin
G
7

Maintain global reference for PopUpWindow and override onBackPressed()...

@Override
public void onBackPressed() {
    if (popupWindow != null && popupWindow.isShowing()) {
        popupWindow.dismiss();
    } else {
        super.onBackPressed();
    }
}

To dismiss by the same Button...

    ivmainmenu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(popupWindow != null && popupWindow.isShowing()) {
                popupWindow.dismiss();
                popupWindow = null;
            } else {
                // show pop up now
            }
        }
    });
Graze answered 20/2, 2014 at 5:18 Comment(2)
Is there any solution to close the popup window by the same button?Savoirvivre
@Savoirvivre That was caused by onBackPressed event was captured by the hosting activity not the popup window.Ilo
E
2

Try this way: Implement onBackPressed() and add

if(popup!=null) {
    popup.dismiss();
    popup=null;
}

And set your PopWindow with below:

popup.setOutsideTouchable(true);
Evangelical answered 20/2, 2014 at 5:17 Comment(1)
@Savoirvivre You will get ACTION_OUTSIDE when the touch is outside the activity. but this is only for my requirementEvangelical
Q
2

please write onBackPressed() and have following code

if(popup!=null){
   //dismiss the popup
   popup.dismiss();
   //make popup null again
   popup=null;
}
Quits answered 20/2, 2014 at 5:19 Comment(0)
W
0

You can override onBackPressed() callback in your code and check to see if your pop-up is already showing(then dismiss it), else you call super to get normal behavior.

Westing answered 20/2, 2014 at 5:18 Comment(0)
B
0

Try this..

Use PopupWindow popupWindow as Global variable

use popup.setOutsideTouchable(true);

@Override
public void onBackPressed() {
    if (popupWindow != null) {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    } else {
        finish();
    }
}
Busty answered 20/2, 2014 at 5:19 Comment(1)
Probably safer to use if (popupWindow != null && popupWindow.isShowing())Pekingese

© 2022 - 2024 — McMap. All rights reserved.