Skip to content Skip to sidebar Skip to footer

Close Android Popup Window With Back Press

I have created a android application where I have created a popup screen. But when I am pressing the back button, popup is not getting closed. I have tried with onBackPressed(). I

Solution 1:

What you need to do is call setBackgroundDrawable on your PopupWindow after you initialize it. Something like:

myPopup.setBackgroundDrawable(newBitmapDrawable());

Solution 2:

Recently I worked with ListPopupWindow (android.support.v7.internal.widget.ListPopupWindow) and back button started to work when I called

popupWindow.setModal(true);

no matter what I set in setBackgroundDrawable method as other solutions suppose here.

Solution 3:

LayoutInflaterlayoutInflater= (LayoutInflater)MainActivity.this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    ViewpopupView= layoutInflater.inflate(R.layout.popup_window_country_list, null);
    countryPopup = newPopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    countryPopup.setBackgroundDrawable (newBitmapDrawable());

    countryPopup.setFocusable(true); //Make Here True For back press dismiss

    countryPopup.setOutsideTouchable(true); 

    countryPopup.setTouchInterceptor(newOnTouchListener() {

        publicbooleanonTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {

                countryPopup.dismiss();


                returntrue;

            }

            returnfalse;

        }
    });

Solution 4:

100% popup will dismiss on back press. Replace your Popup code with this below code

publicvoidpopup() {

    ViewpopUpView_pur= getActivity().getLayoutInflater().inflate(R.layout.popup, null);
    PopupWindowpopuplayout_pur=newPopupWindow(popUpView_pur, -1, -1, true);
    popuplayout_pur.setBackgroundDrawable(newBitmapDrawable());
    popuplayout_pur.setOutsideTouchable(true);
    popuplayout_pur.showAtLocation(popUpView_pur, 17, 0, 0);

}

(or)

publicvoidpopup() {
    // TODO Auto-generated method stubLayoutInflaterinflater= (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewpopupView= inflater.inflate(R.layout.popuplayout, null, false);

    PopupWindowpw=newPopupWindow(getActivity());
    pw.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
    pw.setHeight(WindowManager.LayoutParams.MATCH_PARENT);

    pw.setTouchable(true);
    pw.setFocusable(true);
    pw.setOutsideTouchable(true);
    pw.setContentView(popupView);

    pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}

Solution 5:

//here "popUp" is ref of PopupWindow

popUp.setBackgroundDrawable(newBitmapDrawable());// it is most important peace of code// For Key ListenersViewv= popUp.getContentView();

//Here assigning the key Listners

    v.setOnKeyListener(this);

    @OverridepublicbooleanonKey(View v, int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK) popUp.dismiss();

        returnfalse;

    }//Implements the KeyListener//and be careful we should implement "OnKeyListener"`

I hope it is useful (I'm the new user)

Post a Comment for "Close Android Popup Window With Back Press"