11

hi i have made a n activity in that i open an activity as a Dialog popup on another activity ,SO i g.ot the problem it is of my theme,

 <item `name="android:windowCloseOnTouchOutside">@bool/config_closeDialogWhenTouchOutside</item>`

but i dont know what value should i change inside "windowCloseOnTouchOutside" so that i can do my activity dialog set cancellable false..? My code in theme is as below:

 <style name="Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
        <item name="android:windowBackground">@android:drawable/panel_background</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:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:windowCloseOnTouchOutside">@bool/config_closeDialogWhenTouchOutside</item>
        <item name="android:windowActionModeOverlay">true</item>

        <item name="android:colorBackgroundCacheHint">@null</item>

        <item name="textAppearance">@android:style/TextAppearance</item>
        <item name="textAppearanceInverse">@android:style/TextAppearance.Inverse</item>

        <item name="textColorPrimary">@android:color/primary_text_dark</item>
        <item name="textColorSecondary">@android:color/secondary_text_dark</item>
        <item name="textColorTertiary">@android:color/tertiary_text_dark</item>
        <item name="textColorPrimaryInverse">@android:color/primary_text_light</item>
        <item name="textColorSecondaryInverse">@android:color/secondary_text_light</item>
        <item name="textColorTertiaryInverse">@android:color/tertiary_text_light</item>
        <item name="textColorPrimaryDisableOnly">@android:color/primary_text_dark_disable_only</item>
        <item name="textColorPrimaryInverseDisableOnly">@android:color/primary_text_light_disable_only</item>
        <item name="textColorPrimaryNoDisable">@android:color/primary_text_dark_nodisable</item>
        <item name="textColorSecondaryNoDisable">@android:color/secondary_text_dark_nodisable</item>
        <item name="textColorPrimaryInverseNoDisable">@android:color/primary_text_light_nodisable</item>
        <item name="textColorSecondaryInverseNoDisable">@android:color/secondary_text_light_nodisable</item>
        <item name="textColorHint">@android:color/hint_foreground_dark</item>
        <item name="textColorHintInverse">@android:color/hint_foreground_light</item>
        <item name="textColorSearchUrl">@android:color/search_url_text</item>

        <item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
        <item name="textAppearanceMedium">@android:style/TextAppearance.Medium</item>
        <item name="textAppearanceSmall">@android:style/TextAppearance.Small</item>
        <item name="textAppearanceLargeInverse">@android:style/TextAppearance.Large.Inverse</item>
        <item name="textAppearanceMediumInverse">@android:style/TextAppearance.Medium.Inverse</item>
        <item name="textAppearanceSmallInverse">@android:style/TextAppearance.Small.Inverse</item>

        <item name="listPreferredItemPaddingLeft">10dip</item>
        <item name="listPreferredItemPaddingRight">10dip</item>
        <item name="listPreferredItemPaddingStart">10dip</item>
        <item name="listPreferredItemPaddingEnd">10dip</item>

        <item name="preferencePanelStyle">@style/PreferencePanel.Dialog</item>
    </style>
1
  • add <item name="android:windowCloseOnTouchOutside">false</item> Apr 6, 2018 at 9:35

3 Answers 3

43

You can do that in Java coding very easily. If you have an Activity (even if it looks like a Dialog), then you should do

this.setFinishOnTouchOutside(false);

if you have used Dialog class, you should call

dialog.setFinishOnTouchOutside(false) 

if you want to prevent closing it open when the background activity is clicked.

5
  • "setFinishOnTouchOutside(false)" on which object brother?
    – jigar
    Oct 7, 2013 at 8:03
  • 1
    If you are poping up a dialog using Dialog class, then dialogObject.setCanceledOnTouchOutside(false); otherwise this.setCanceledOnTouchOutside(false); Oct 7, 2013 at 8:06
  • @umer-sure brother...sure..i frgot it...:)
    – jigar
    Oct 7, 2013 at 10:47
  • But is it possible to click the background items? @UmerFarooq
    – Happy
    Dec 29, 2017 at 15:48
  • setFinishOnTouchOutside(false); prevent outside click but dnt prevent back button click :( Mar 24, 2021 at 1:03
1

This could help you. It is a way to handle the touch outside event:

How to cancel an Dialog themed like Activity when touched outside the window?

There is a way to get dialog-like "touch outside to cancel" behaviour from an Activity themed as a dialog, though I've not fully investigated whether it has unwanted side effects.

Within your Activity's onCreate() method, before creating the view, you're going to set two flags on the window: One to make it 'non-modal', to allow views other than your activity's views to receive events. The second is to receive notification that one of those events has taken place, which will send you an ACTION_OUTSIDE move event.

Try as below:

public class MyActivity extends Activity {

 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Make us non-modal, so that others can receive touch events.
    getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);

    // ...but notify us that it happened.
    getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

    // Note that flag changes must happen *before* the content view is set.
    setContentView(R.layout.my_dialog_view);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // If we've received a touch notification that the user has touched
    // outside the app, finish the activity.
    if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
      finish();
      return true;
    }

    // Delegate everything else to Activity.
    return super.onTouchEvent(event);
  }
}
0
0

The best way is this code:

AlertDialog.Builder alert = new AlertDialog.Builder(Activity.this);
alert.setCancelable(false);
1
  • 1
    it just work perfectly fine with AlertDialog.Builder ,call setCancelable before cereating AlertDilog. Nov 10, 2018 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.