How do I display a full-width DialogFragment?

janoliver picture janoliver · May 26, 2013 · Viewed 10.6k times · Source

I am trying to create a custom DialogFragment, that extends over the whole width of my screen (or rather, parent fragment). Although I can make the borders of the DialogFragment transparent, there still is a padding on the right and left that I cannot get rid of.

This is my Fragment:

public static class LoaderDialog extends DialogFragment {

    static LoaderDialog newInstance() {
        LoaderDialog f = new LoaderDialog();

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.loader_f, container, false);

        WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
        p.y = getSupportActionBar().getHeight();

        getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        getDialog().getWindow().setGravity(Gravity.TOP);
        getDialog().getWindow().setAttributes(p);

        return view;
    }
}

This is a picture, how it looks like: screenshot

As you can see, the DialogFragment (the red thing) has some margins on the side. I want those to be gone. Any idea how to do this (in java, if possible)?

Answer

Dirk picture Dirk · Oct 18, 2014

You can use:

WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.FILL_HORIZONTAL; 

Full example:

public class TextEditor extends DialogFragment {

    public TextEditor () {

    }

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

        View view = inflater.inflate(R.layout.fragment_text_editor, container);

        WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
        wmlp.gravity = Gravity.FILL_HORIZONTAL;

        return view;
    }
}