Search code examples
androidandroid-dialogfragment

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.


Solution

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