Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment overlap by status bar

I am little confused.

At first, when I create a toolbar and it get overlapped by status bar, I just add fitSysmtemWindow="true" in parent XML and it work just fine.

But when I create FullScreen DialogFragment, It get overlapped by status bar too. I tried to add fitSystemWindow="true" and it doesn't work.

Only present on android 5.0+. Didn't set status bar to translucent anywhere.

Here my DialogFragment code

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_create_bill_dialog,container, false);

    return view;
}

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

Thanks.Sorry for my bad Eng.

like image 675
Math Burn Avatar asked Oct 30 '15 04:10

Math Burn


People also ask

Is DialogFragment deprecated?

This method is deprecated. androidx.

What is the difference between fragment and DialogFragment?

DialogFragment is Fragment so you have to manage them like Fragment. You can insert them in your layout for example. But you definitely have to manage them as you manage fragment. The only difference is when creating them you use onCreateDialogFragment instead of onCreate and onCreateView.

How to show DialogFragment in android?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

How do you dismiss a DialogFragment?

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. Dismiss the fragment and its dialog.


1 Answers

I had the same problem. I resolved it by adding 25dp padding to the top of my Dialog Fragment root view, to ensure it didn't overlap with the status bar.

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //If showing as a dialog, add some padding at the top to ensure it doesn't overlap status bar
    if (getDialog() != null) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        view.setPadding(0, (int)PresentationUtils.convertDpToPixel(25f, getContext()), 0, 0);
        view.requestLayout();
    }
...
}

Btw, the util method I'm using for converting dp to px can be found here Converting pixels to dp

like image 183
aaronmarino Avatar answered Nov 07 '22 17:11

aaronmarino