Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setCanceledOnTouchOutside doesn't work when i tap just near the dialog on 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 is working only when i tap on a certain distance from the dialog.When i tap just near the dialog it doesn't get dismissed.Do you guys know a way to dismiss the dialog even when i tap just near the dialog?Thanks.

like image 218
user2255427 Avatar asked Aug 14 '13 15:08

user2255427


People also ask

What is the effect of setCancelable false?

setCancelable(false) means that back key doesn't close the dialog.

What is setCanceledOnTouchOutside?

setCanceledOnTouchOutside only prevents dismissing by clicking outside of the dialog. But you can still dismiss it with back button for instance. If you don't want your dialog to be cancelable at all use dialog.setCancelable(false)


1 Answers

Using AlertDialog.THEME_HOLO_LIGHT works if you want the dialog to be full screen. An alternative is to create your own style, like so:

  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);

have style.xml in values folder like 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 in values folder:::

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

This is working for me. Hope it will works for you as well

like image 148
King of Masses Avatar answered Oct 04 '22 21:10

King of Masses