Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] How to dismiss Android Dialog when click outside (using FLAG_NOT_FOCUSABLE)

Posted on November 11, 2022 By

Solution 1 :

1 – Set the flag-FLAG_NOT_TOUCH_MODALfor your dialog’s window attribute

Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

2 – Add another flag to windows properties,,FLAG_WATCH_OUTSIDE_TOUCH– this one is for dialog to receive touch event outside its visible region.

3 – Override onTouchEvent()of dialog and check for action type. if the action type is ‘MotionEvent.ACTION_OUTSIDE‘ means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform. view plainprint?

public boolean onTouchEvent(MotionEvent event)  
{  

       if(event.getAction() == MotionEvent.ACTION_OUTSIDE){  
        System.out.println("TOuch outside the dialog ******************** ");  
               this.dismiss();  
       }  
       return false;  
}  

Problem :

I have a dialog that I want to be dismissed when I click outside it. However, I don’t want it to receive key events, because it’s a volume dialog, and I want the activity to be able to receive the key volume up and down events. So I setted FLAG_NOT_FOCUSABLE, as you see below:

    AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.MyDialogTheme);
    AlertDialog alert = builder.create();
    Window window = alert.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    alert.setView(view);

the problem is that, with this flag, I also can’t make the dialog dismiss when a touch outside it happens.

All the solutions on How to dismiss the dialog with click on outside of the dialog? like dialog.setCanceledOnTouchOutside(true); wont work in this case.

READ  [FIXED] detect middle (rotary) button click on android waer
Powered by Inline Related Posts
Android Tags:android, android-layout

Post navigation

Previous Post: [FIXED] java – How can I solve this Android Studio Firebase problem?
Next Post: [FIXED] Android Java Publish result cast issue at MyCustomAdapter.java

Related Posts

[FIXED] How to completely hide the ui in my android app Android
[FIXED] android – User state management based on Google account Android
[FIXED] android studio – AndroidStudio Kotlin connect to bluetooth Android
[FIXED] android – How to play mp4 via exoplayer? Android
[FIXED] android – Dart/Flutter App only throws Exception when Closing and Reopening in Emulator Android
[FIXED] indexoutofboundsexception – Android production app, rare out-of-bounds-exception crash Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme