Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Dialogfragment disable click outside on android?

Tags:

android

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.

like image 862
chohyunwook Avatar asked May 17 '17 07:05

chohyunwook


People also ask

How do you dismiss dialog with click on outside of dialog?

You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog. Window window = this.

Is DialogFragment deprecated?

This method is deprecated. androidx. activity.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

What is DialogFragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


2 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);
like image 60
IntelliJ Amiya Avatar answered Nov 05 '22 10:11

IntelliJ Amiya


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
}
like image 40
Hamid Zandi Avatar answered Nov 05 '22 08:11

Hamid Zandi