8

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(). It is not working.

Can someone tell me what to do.

Regards,

Shankar

Bhabani Shankar
  • 1,267
  • 4
  • 22
  • 41
  • 2
    It would help if you provided the code snippet that you wrote. We don't know if you are refering to an instance of Dialog or PopupWindow. – IgorGanapolsky Dec 22 '11 at 16:36

8 Answers8

38

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

myPopup.setBackgroundDrawable(new BitmapDrawable());
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • 2
    1. Make sure you call this *before* you show the popup. 2. Might want to pass a Resources object into that constructor, since the constructor with no parameters is apparently deprecated. 3. More info: http://stackoverflow.com/questions/3121232/android-popup-window-dismissal – SilithCrowe Mar 30 '12 at 15:23
  • 2
    This works great. But ensure you have focusable set to true. If it false then back button wont work automaticallt, you will need to implement hotveryspicy's solution in the calling activity. – jim Nov 27 '12 at 10:53
  • Not sure why this works, but it does work along with the normal code to dismiss the popup. – kabuto178 Jun 01 '14 at 19:27
  • 4
    Note that it doesn't have to be a BitmapDrawable, I've used a ColorDrawable which will quite happily be constructed with no parameters – Newtz Jan 08 '15 at 00:59
8

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.

Berťák
  • 7,143
  • 2
  • 29
  • 38
4
LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup_window_country_list, null);
    countryPopup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    countryPopup.setBackgroundDrawable (new BitmapDrawable());

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

    countryPopup.setOutsideTouchable(true); 

    countryPopup.setTouchInterceptor(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {

                countryPopup.dismiss();


                return true;

            }

            return false;

        }
    });
user3442433
  • 53
  • 1
  • 6
3

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

public void popup() {

    View popUpView_pur = getActivity().getLayoutInflater().inflate(R.layout.popup, null);
    PopupWindow popuplayout_pur = new PopupWindow(popUpView_pur, -1, -1, true);
    popuplayout_pur.setBackgroundDrawable(new BitmapDrawable());
    popuplayout_pur.setOutsideTouchable(true);
    popuplayout_pur.showAtLocation(popUpView_pur, 17, 0, 0);

}

(or)

public void popup() {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.popuplayout, null, false);

    PopupWindow pw = new PopupWindow(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);
}
sigaram
  • 31
  • 2
2
//here "popUp" is ref of PopupWindow

popUp.setBackgroundDrawable(new BitmapDrawable());// it is most important peace of code

// For Key Listeners

View v = popUp.getContentView();

//Here assigning the key Listners

    v.setOnKeyListener(this);

    @Override   
    public boolean onKey(View v, int keyCode, KeyEvent event) {

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

        return false;

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

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

K.C.
  • 2,084
  • 2
  • 25
  • 38
knvchowdary
  • 81
  • 2
  • 14
2

Following code is working perfectly. So override following function in your activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     //Changes 'back' button action
    if(keyCode==KeyEvent.KEYCODE_BACK)
    {
        if(!popUpHelper.isPopupShowing()){
            onBackPressed();
        }else{
            popUpHelper.dismiss();
        }
    }

    return super.onKeyDown(keyCode, event);
}


class PopupHelper {
 PopupWindow popupWindowAttachment;
  public void initAndShow(Activity context,int mToolbarHeight){

   View layout = activity.getLayoutInflater().inflate(
            R.layout.activity_chat_attachment_popup, null);
    //create and show and Make sure popup window focusable should be false
    popupWindowAttachment = new PopupWindow(layout,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindowAttachment.showAsDropDown(layout, 0, mToolbarHeight
            + (mToolbarHeight / 2) - 5);

  }

  public void dismiss() {
    if (isPopupShowing())
            popupWindowAttachment.dismiss();
  }
  public boolean isPopupShowing() {
    return popupWindowAttachment==null?false:popupWindowAttachment  
                 .isShowing();
  }
}
turbandroid
  • 2,296
  • 22
  • 30
0

The popup window normally closes when you press the back button. if you need to handle certain things when the popup closes, you can use

popupWindow.setOnDismissListener

Note:- I worked in kotlin. It worked for me

Praveen
  • 294
  • 4
  • 9
-1
popup.setBackgroundDrawable(new BitmapDrawable());

popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //do your code here
            }
        });
Dheerendra Mitm
  • 182
  • 1
  • 9