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..
Asked
Active
Viewed 9,462 times
1

Sangeetha Pinto
- 21
- 1
- 6
-
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 Answers
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;
}
});
-
Thanks @jinnings. it's help me to prevent closing popup when I back click. – reegan29 Jul 19 '19 at 11:59
-
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
-
setCancelable(true) will cancel pop window even on touching outside the window's bound – jaimin Aug 12 '14 at 05:02
-
It will only cancel when you press back button or you touch outside of window dialog .. – AndroidHacker Aug 12 '14 at 05:18
-
1SetCancelable(true) is applicable only for the dialog. I'm using pop up window – Sangeetha Pinto Aug 12 '14 at 05:33
-
Hi thanks for the answer, but it worked by setting setTouchable(false); – Sangeetha Pinto Aug 12 '14 at 05:53
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
-
1But 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
-
@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
-