SetCanceledOnTouchOutside does not work when I click next to the dialog on the outside

I have an AlertDialog:

AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT); ListAdapter adapter = new CustomDialogAdapter(context, itemsList); builder.setAdapter(adapter, listener); builder.setTitle(title); AlertDialog dialog = builder.create(); dialog.setCanceledOnTouchOutside(true); 

setCanceledOnTouchOutside only works when I'm at a certain distance from the dialog. When I touch next to the dialogue, it will not be fired. Do you guys know a way to dismiss a dialog, even when I touch only the dialog box? Thanks.

+6
source share
1 answer

Using AlertDialog.THEME_HOLO_LIGHT works if you want the dialog to be fullscreen. An alternative is to create your own style, for example:

  AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.ThemeDialogCustom); ListAdapter adapter = new CustomDialogAdapter(context, itemsList); builder.setAdapter(adapter, listener); builder.setTitle(title); AlertDialog dialog = builder.create(); dialog.setCanceledOnTouchOutside(true); 

there is a style.xml folder in the values, for example below:

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="ThemeDialogCustom" parent="android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowBackground">@color/transparent_color</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> <item name="android:colorBackgroundCacheHint">@null</item> </style> </resources> 

also add colors.xml to the values ​​folder:

 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="transparent_color">#00000000</color> </resources> 

This works for me. Hope this works for you too.

+1
source

Source: https://habr.com/ru/post/951736/


All Articles