1

I want to dismiss the pop up window when back button is pressed. I tried with this code: popwindow.setBackgroundDrawable(new BitmapDrawable()); and it works. But in my app, pop should remain, even after touching outside of the pop up window. It should be dismissed only when back button is pressed. So I tried this: popwindow.setFocusable(false); Now its not dismissing when touched outside the pop up. But its not dismissing on back press too. I do not want to overide `onBackPressed(). Is there any other way, through which i can achieve this. Thanks in advance..

  • handle back press then – KOTIOS Aug 12 '14 at 04:38
  • You may find solution in these links: [1 Close Android popup window with back press][1] [2 dismiss the popup window by back button][2] [1]: http://stackoverflow.com/questions/6940119/close-android-popup-window-with-back-press [2]: http://stackoverflow.com/questions/21898723/dismiss-the-popup-window-by-back-button – UperOne Aug 12 '14 at 04:43
  • possible duplicate of [Android popup window dismissal](http://stackoverflow.com/questions/3121232/android-popup-window-dismissal) – Haresh Chhelana Aug 12 '14 at 04:46

6 Answers6

3
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
new ScaleInAnimation(popupView).animate();
popupWindow.getContentView().setFocusableInTouchMode(true);
popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            popupWindow.dismiss();
                return true;
            }
            return false;
        }
    });
Ben-J
  • 1,084
  • 8
  • 24
jinnings
  • 97
  • 1
  • 3
  • 13
2

Set like this..

    popupWindow.setOutsideTouchable(true); 
    popupWindow.setTouchable(true); 
    popupWindow.setBackgroundDrawable(new BitmapDrawable());           popupWindow.setTouchInterceptor(new OnTouchListener() 
{ 
@Override 
public boolean onTouch(View v, MotionEvent event) 
{ 
if (AppContext.isDebugMode()) 
Log.d("POPUP_WINDOW", "v: "+v.getTag() + " | event: "+event.getAction());
 popupWindow.dismiss(); return true; 
} 
});
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
2

To disable pop up dismiss when click outside of popwindow set

popwindow.setCanceledOnTouchOutside(false);

and for dismiss it on back button set

popwindow.setCancelable(true);
Yograj Shinde
  • 839
  • 12
  • 23
  • 1
    But I'm not getting both properties. it's giving error as setCancelable cannot be resolved. – Sangeetha Pinto Aug 12 '14 at 04:54
  • according to [document](http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean)) this is not possible "Sets whether this dialog is canceled when touched outside the window's bounds. If setting to true, the dialog is set to be cancelable if not already set." – jaimin Aug 12 '14 at 05:00
  • 2
    But that is for the dialog. I'm using a pop up window – Sangeetha Pinto Aug 12 '14 at 05:06
  • @SangeethaPinto try this pw.setOutsideTouchable(false); pw.setTouchable(true); – Yograj Shinde Aug 12 '14 at 05:18
  • If I add that, it dismisses when back button is pressed. But it also gets dismissed, when touched outside the pop up window. i dont want it to dismiss when touched outside the pop up window – Sangeetha Pinto Aug 12 '14 at 05:30
  • Hi it worked by setting popUpWindow.setTouchable(false); thank you for helping me to find the solution. – Sangeetha Pinto Aug 12 '14 at 05:44
1

Aside from the setOutsideTouchable(true) and setFocusable(true) I had to add

popUpView.setBackgroundDrawable(new BitmapDrawable())

to make it work. This did not change the UI of my popup, but for some magical reason, enabled the back button functionality.

Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27
Chad
  • 96
  • 4
1

The code below may be helpfull:

popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(mParent);

And,the show of the popupWindow (popupWindow.showAsDropDown or popupWindow.showAtLocation )must be called in the end.

Maoran Bian
  • 53
  • 1
  • 7
-2

Set Popup property as..

      popup.setOutsideTouchable(false);
      popup.setCancelable(true);
Femil Shajin
  • 1,768
  • 6
  • 24
  • 38
  • But I'm not getting both properties. it's giving error as setCancelable cannot be resolved – Sangeetha Pinto Aug 12 '14 at 04:54
  • according to [document](http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean)) this is not possible "Sets whether this dialog is canceled when touched outside the window's bounds. If setting to true, the dialog is set to be cancelable if not already set." – jaimin Aug 12 '14 at 05:01
  • Not worked first one & There is no such property popup.setCancelable() – VVB Jan 29 '16 at 07:07