Dismissing the dialog by outside touch event is not working

1.4k views Asked by At

I have an utils class which vll return a dialog to the activity in my application. In that i need to provide the user to dismiss the dialog on outside click.So here i used like this

lDialog.setCancelable( true );
lDialog.setCanceledOnTouchOutside( true );

I refereed some of these links Here & here

Even i checked with these flags as well

lDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            lDialog.getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

But it is not working for me.. i refer too many answers from SOF but unfortunately nothing is worked for me..

Here is my full set of code which i tried so far. It is working perfect except the outside touch dismiss event..

 public static Dialog createDialog( Context context,
            int viewId )
    {
        Dialog lDialog = new Dialog( context,
                                     AlertDialog.THEME_HOLO_LIGHT );
        lDialog.getWindow().setBackgroundDrawable( new ColorDrawable( 1 ) );
        lDialog.requestWindowFeature( Window.FEATURE_NO_TITLE );

        lDialog.setContentView( viewId );
        lDialog.setCancelable( true );
        lDialog.setCanceledOnTouchOutside( true );
        lDialog.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );


        return lDialog;
    }

Sorry for my poor English!! Thank you.

2

There are 2 answers

0
King of Masses On BEST ANSWER

Here, Finally i am going to answer my question.

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

 public static Dialog createDialog( Context context,
            int viewId )
    {
        Dialog lDialog = new Dialog( context,
                                     R.style.ThemeDialogCustom );
        lDialog.getWindow().setBackgroundDrawable( new ColorDrawable( 1 ) );
        lDialog.requestWindowFeature( Window.FEATURE_NO_TITLE );

        lDialog.setContentView( viewId );
        lDialog.setCancelable( true );
        lDialog.setCanceledOnTouchOutside( true );
        lDialog.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );

        return lDialog;
    }

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

0
reidzeibel On

It is possible that the dialog uses up the entire screen, hence there is no "outside" click. Have you tried making a very simple dialog and then test outside click.

I have never used these setFlags method :

    lDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    lDialog.getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

or this :

 WindowManager.LayoutParams lp2 = new WindowManager.LayoutParams();
 lp2.copyFrom( lDialog.getWindow().getAttributes() );
 lp2.width = WindowManager.LayoutParams.MATCH_PARENT;
 lp2.height = WindowManager.LayoutParams.MATCH_PARENT;
 lDialog.getWindow().setAttributes( lp2 );

and I have no problem using setCanceledOnTouchOutside