Allow external touch for DialogFragment

I have a Fragment in my application that shows DialogFragment .
I have a button in the fragment that closes the dialog. But when I show dialogFragment, touching outside the dialog does nothing, and I can’t click on the buttons outside the dialog fragment.

How to allow external touches for DialogFragment?

+14
android android-dialogfragment
Mar 13 '13 at 10:21
source share
6 answers

To do this, you must enable the Window flag, which allows external touch, and for a good appearance, the background flag must be cleared. Since this needs to be done after creating the dialog, I implemented it using the Handler .

 @Override public void onViewCreated(final View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // This is done in a post() since the dialog must be drawn before locating. getView().post(new Runnable() { @Override public void run() { Window dialogWindow = getDialog().getWindow(); // Make the dialog possible to be outside touch dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); getView().invalidate(); } }); } 

At this point, external contact is possible.

If we want to make it more enjoyable and without a frame, we can add the following code:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Hide title of the dialog setStyle(STYLE_NO_FRAME, 0); } 
+18
Mar 14 '13 at 14:18
source share

This is a more general flag, allowing any action not only to touch actions

 window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); 
+4
Dec 20 '14 at 12:26
source share

I have found an alternative way to handle dismissal by touching outside. Please check below code example that will definitely work,

 @Override public void onStart() { // mDialogView is member variable mDialogView = getView(); mDialogView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { float eventX = event.getRawX(); float eventY = event.getRawY(); int location[] = new int[2]; mDialogView.getLocationOnScreen(location); if (eventX < location[0] || eventX > (location[0] + mDialogView.getWidth()) || eventY < location[1] || eventY > location[1] + mDialogView.getHeight()) { dismiss(); return true; } return false; } }); } 

In my case, I was getting getDialog () as a non-zero value, but getDialog (). Elimination () does not work. Also, the solution marked as above also does not work in my case. So, I took this approach.

+1
Jan 20 '15 at 10:28
source share

You need to set the right style for your needs. You can read more about styles for FragmentDialogs in official docs.

I believe you can use the following style:

  • STYLE_NO_FRAME
0
Mar 13 '13 at 11:51
source share

Well, let it be specific. If you want to work with external views, you should not use a dialog fragment, use a fragment instead.

  FragmentTransaction transaction = fragmentManager.beginTransaction(); // For a little polish, specify a transition animation transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); // To make it fullscreen, use the 'content' root view as the container // for the fragment, which is always the root view for the activity transaction.add(android.R.id.content, newFragment) .addToBackStack(null).commit(); 

And make a wrap_content layout, which, apparently, will look like Dialog, and by default it is placed at the top of the screen.

See here for more details.

0
Mar 04 '16 at 11:36
source share

@yaniv provided an answer. But in case you want, here is a snippet where you get the same result, but without having to publish Runnable to the root View :

 @NonNull @Override public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) { final Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); return dialog; } 
0
Jul 18 '19 at 12:04 on
source share



All Articles