I have implemented a custom dialog for my application. I want to implement that when the user clicks outside the dialog, the dialog will be dismissed. What do I have to do for this?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Or, if you're customizing the dialog using a theme defined in your style xml, put this line in your theme:
Another solution this code was cutted from android source code of
Window
You should just add this Two methods to your dialog source code.This solution doesnt have this problem :
Call
dialog.setCancelable(false);
from your activity/fragment.You can use
dialog.setCanceledOnTouchOutside(true);
which will close the dialog if you touch outside of the dialog.Something like,
Or if your Dialog in non-model then,
1 - Set the flag-
FLAG_NOT_TOUCH_MODAL
for your dialog's window attribute2 - Add another flag to windows properties,,
FLAG_WATCH_OUTSIDE_TOUCH
- this one is for dialog to receive touch event outside its visible region.3 - Override
onTouchEvent()
of dialog and check for action type. if the action type is 'MotionEvent.ACTION_OUTSIDE
' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform. view plainprint?For more info look at How to dismiss a custom dialog based on touch points? and How to dismiss your non-modal dialog, when touched outside dialog region
to close dialog on touch outside.
And if you don't want to close on touch outside, use the code below:
You can use this implementation of onTouchEvent. It prevent from reacting underneath activity to the touch event (as mentioned howettl).
Source: http://blog.twimager.com/2010/08/closing-activity-by-touching-outside.html