Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment allowing clicking activity behind

I started a DialogFragment from ActivityA, When i click on the white background of DailogFragment where there are no elements, the click event is happening on the ActivityA which is in background

public class FilterDialog extends DialogFragment{

    private static final String TAG = "FilterDialog";
    Calendar myCalendar = Calendar.getInstance();
    private String stringDate;


    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
            monthOfYear++;
            stringDate = dayOfMonth+"-"+ monthOfYear +"-"+year;
        }

    };

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

        View rootView = inflater.inflate(R.layout.dialog_filter, container, false);

        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        toolbar.setTitle("Filter");
        Button datePicker = (Button) rootView.findViewById(R.id.datePicker);
        View.OnClickListener datePickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(v.getId() == R.id.datePicker){
                    new DatePickerDialog(v.getContext(), date, 
                                   myCalendar.get(Calendar.YEAR),
                                   myCalendar.get(Calendar.MONTH),
                                   myCalendar.get(Calendar.DAY_OF_MONTH)).show();

                }
            }
        };
        datePicker.setOnClickListener(datePickListener);

        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
        }
        return rootView;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onStart()
    {
        super.onStart();
        final AlertDialog d = (AlertDialog)getDialog();
        if(d != null)
        {
            Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Boolean wantToCloseDialog = false;

                    if(wantToCloseDialog)
                        d.dismiss();

                }
            });
        }
    }
}

This is my DialogFragment, to show dialogfragment i used

FragmentManager fragmentManager = getSupportFragmentManager();
FilterDialog newFragment = new FilterDialog();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.replace(android.R.id.content, newFragment).addToBackStack(null).commit();

screenshot

As shown in the above screenshot, If i click on any of the highlighted color, click is happening on ActivityA which is in background and not visible. Can anyone help me to disable this.

like image 286
Sai Phani Krishna Uppalapati Avatar asked Nov 10 '16 12:11

Sai Phani Krishna Uppalapati


People also ask

Is DialogFragment deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

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.

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.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.


2 Answers

Make android:clickable=true for the root layout of that dialog.

like image 127
Anirudh Goud Avatar answered Oct 17 '22 00:10

Anirudh Goud


You have set setCancelable(false); in onCreateView method, next to the inflate line as below

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

        View rootView = inflater.inflate(R.layout.dialog_filter, container, false);
        setCancelable(false);
}

Hope this is helpful :)

like image 45
Jeevanandhan Avatar answered Oct 17 '22 01:10

Jeevanandhan