Home » Android » How to Dialogfragment disable click outside on android?

How to Dialogfragment disable click outside on android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I use extends DialogFragment

I want except dialog, disable click on android.

I try setCanceledOnTouchOutside(true) but it doesn’t not work.

and please check my source.

public class QuestDialog extends DialogFragment {
   private String mainMessage = "";
   private View.OnClickListener positiveListener = null;
   private View.OnClickListener negativeListener = null;

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

   View view = inflater.inflate(R.layout.main_dialog, container, false);

   TextView textView = (TextView) view.findViewById(R.id.main_msg);
   textView.setText(mainMessage);

   Button positiveBtn = (Button) view.findViewById(R.id.dialog_positive_btn);
   Button negativeBtn = (Button) view.findViewById(R.id.dialog_negative_btn);

   positiveBtn.setOnClickListener(new View.OnClickListener() {
       @Override 
       public void onClick(View v) {
          dismiss();
          if (positiveListener != null) { 
              positiveListener.onClick(view);
          }
       }
   });
   negativeBtn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
          dismiss();

          if (negativeListener != null) [
              negativeListener.onClick(view);
          }
       }
    });
    return view;
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {

    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setCanceledOnTouchOutside(true);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
    return dialog;
 }
 public void setMessage(final String msg) {
     mainMessage = new String(msg);
 }

 public void setPositiveButton(View.OnClickListener listener) {
     positiveListener = listener;
 }

this one.

How to disable click of dialogfragment outside on android?

thanks.

How to&Answers:

For your requirement, you should use setCancelable(true).

Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

Answer:

Kotlin
overide onCreateDialog in your DialogFragment

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)
    dialog.setCancelable(false)
    dialog.setCanceledOnTouchOutside(false)
    return dialog
}

Answer:

Set .setCancelable(false) in your DialogFragment before show.

QuestDialog questDialog = new QuestDialog();
questDialog.setCancelable(false);
questDialog.show(getSupportFragmentManager(),"Tag");

Answer:

Plain setCancelable(false) (without Dialog.) in onCreateView worked for me

Answer:

Please use dialog.setCancelable(boolean) method.
Thanks

Answer:

write setCancelable(false); in onCreateView of DialogFragment.
Thats it.