2

am using a Dialog with a custom view. That view's layout is 300dp x 300dp is size and centered on the screen. Now i want it to be dismissed when the user touches outside the dialog. I thought: great! there is a function for this: setCanceledOnTouchOutside(boolean). But i has no effect. It simply doesn't work. I also tried to add an onTouchListener and listen for events that are outside the dialog, but those events doesn't seem to be captured by the listener :(

Any ideas why this doesn't work or how it could be done in another way?

Here is how i declare the Dialog:

mMenuDialog = new Dialog(this,
            android.R.style.Theme_Translucent_NoTitleBar);
    mMenuDialog.setContentView(R.layout.menu_dialog);
    mMenuDialog.setCancelable(true);
    mMenuDialog.setCanceledOnTouchOutside(true);

and here is the declaration of the custom view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp" android:layout_height="300dp"
android:background="@drawable/menu_dialog_background"
android:orientation="vertical" android:layout_gravity="center"
android:id="@+id/dialog_root">
...
</LinearLayout>

and here is the used background:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding android:left="15dp" android:top="15dp" android:right="15dp"
    android:bottom="15dp" />
<solid android:color="#f0f0f0" />
<corners android:radius="15dip" />
</shape> 
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Goddchen
  • 4,459
  • 4
  • 33
  • 54

2 Answers2

14

It might be a tad late but for those who are still looking for the answer, here it is.

Apply a custom style to your dialog. In your style, include this:

<item name="android:windowIsFloating">true</item>

Otherwise the dialog will use full screen. Hence no outside area to click to cancel. This works for dialogs with custom content.

Example:

<style name="dialog_theme" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_gravity">center</item>
</style>

Hope this helps.

flip
  • 156
  • 1
  • 2
  • Is there any way to have full screen custom background color (say #55FF0000) with windowIsFloating true? If I set #55FF0000 for windowBackground then I got small area color on dialog edge...not whole screen. Given windowIsFloat false I can get full screen color but setCanceledOnTouchOutside is no longer working... Searched and tried for a while but no luck....please help. – Arst Oct 17 '14 at 16:07
2

i did solve it now by wrapping my custom dialog view layout with an additional LinearLayout and adding an onTouchListener to this wrapping LinearLayout. In that onTouchListener i checked if the click was inside or outside the dialog.

Goddchen
  • 4,459
  • 4
  • 33
  • 54
  • Goddchen can you please explain how do you realised this check if the click was inside or outside? Some come for example will serve me great! Thanks! – Stefan Doychev Oct 10 '11 at 08:36