Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make DialogFragment modal?

I have a DialogFragment :

public static class CharacteristicDialog extends DialogFragment {
        int mNum;

        static CharacteristicDialog newInstance(int num) {
            CharacteristicDialog f = new CharacteristicDialog();            
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);
            return f;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments().getInt("num");
            setStyle(STYLE_NO_INPUT, 0);        
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.characteristic_dialog, container, false);
            v.setOnClickListener(new OnClickListener() {                
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

I create it in my main Fragment like this :

        DialogFragment newFragment = CharacteristicDialog.newInstance(v.getId());
        newFragment.setShowsDialog(true);
        newFragment.show(getFragmentManager(), "dialog");   

It shows well, but it is not modal (I can click on the sides and make actions on my main Fragment behind the dialog).

How do I make it modal ?

Thanks

like image 583
Rémy DAVID Avatar asked Sep 07 '12 16:09

Rémy DAVID


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

What is DialogFragment used for?

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.

What is the difference between dialog and 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.

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.


2 Answers

This worked for me.

myDialogFragment.setCancelable(false);
like image 73
Amit Garg Avatar answered Oct 02 '22 04:10

Amit Garg


Just change STYLE_NO_INPUT то STYLE_NORMAL into your setStyle() method. For more info about DialogFragment styles and themes take look at docs: http://developer.android.com/reference/android/app/DialogFragment.html

like image 30
ashakirov Avatar answered Oct 02 '22 04:10

ashakirov