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 exitin
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.
Try this way: Implement onBackPressed()
and add
if(popup!=null) {
popup.dismiss();
popup=null;
}
And set your PopWindow
with below:
popup.setOutsideTouchable(true);
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
}
}
});
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();
}
}
Replace
popupWindow.setOutsideTouchable(false);
with this
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
please write onBackPressed(
) and have following code
if(popup!=null){
//dismiss the popup
popup.dismiss();
//make popup null again
popup=null;
}