PopupWindow - Dismiss when clicked outside
Please try to set setBackgroundDrawable
on PopupWindow
that should close the window if you touch outside of it.
How do I enable a popup to close when clicked outside of it?
Make your PopupWindow
to wrap_content and make it focusable.
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
// HERE IS WHAT I THOUGHT WOULD MAKE IT BE ABLE TO ENABLE THE OUTSIDE TOUCH
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btn = (Button) popupView.findViewById(R.id.button);
popupWindow.showAsDropDown(popupView, 0, 0);
Dismissing PopupWindow the way PopupMenu can be dismissed by clicking outside
You just need to set setBackgroundDrawable
and setOutsideTouchable
properties of PopupWindow
it should close the window if you touch outside of it.
PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
How to dismiss PopupWindow with touch outside when its mFocusable set false?
it may work
// Removes default background.
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myPopupWindow.setOutsideTouchable(true);
Alternatively:
myPopupWindow.setFocusable(true);
Not sure what the differences are, but the ListPopupWindow source code actually uses the latter when it's modality is set to true with setModal, so at least the Android developers consider this a viable approach, and it's only one line.
BTW, Don't use BitmapDrawable deprecated constructor, use this new ColorDrawable(android.R.color.transparent) to replace default background.
Happy Coding :)
Android Popup Window dismisses when clicked outside
Ok so fixed in the end.
First made the main layout which the popup sits on a relative layout. Then placed a full screen blank layout on top which I made invisible and transparent.
Then show when the popup is shown, set the full screen panel visible with setVisibility(View.VISIBLE);
and hide when popup is hidden with setVisibility(View.GONE);
Also need to return true from an on touch listener for the layout with (To stop touch events passing back to the main layout):
blocker.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
And give the popup window the properties:
setTouchable(true);
setOutsideTouchable(false);
Cheers
Related Topics
How to Save Image in Android Gallery
Android Java.Lang.Noclassdeffounderror
How to Create a Custom Notification Layout in Android
Googlesignatureverifier Signature Not Valid Message (Google Play Services 9.0.0)
Proguard Causing Runtimeexception (Unmarshalling Unknown Type Code) in Parcelable Class
Android:Call Activity of Another Application
How Can Make My Viewpager Load Only One Page at a Time Ie Setoffscreenpagelimit(0);
Is There Any Limit of Bundle in Android
How to Get Available Wifi Networks and Display Them in a List in Android
Adding an Onclicklistener to Listview (Android)
Android SQLite Issue - Table ... Has No Column Named
How to Crop an Image in Android
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
Java.Net.Connectexception: Localhost/127.0.0.1:8080 - Connection Refused