12

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)?

janoliver
  • 7,744
  • 14
  • 60
  • 103
  • possible duplicate of [Android dialog width](http://stackoverflow.com/questions/2634850/android-dialog-width) – user May 27 '13 at 07:22
  • Thank you for the link. However, I tried `p.width = LayoutParams.FILL_PARENT;` before without success. So the answer of UMAR seems not to help me. – janoliver May 27 '13 at 10:38

5 Answers5

14

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;
    }
}
Dirk
  • 2,011
  • 1
  • 20
  • 25
0

try this:

p.horizontalMargin = 0;
Bengerman
  • 821
  • 7
  • 7
0

use this style for DialogFragment

<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>

or use this code in onCreateView method of DialogFragment

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **220**, getResources().getDisplayMetrics());
    getDialog().getWindow().setLayout(width,px);

ps. 220 is DialogFragment height, change it as u wish

Saeed-rz
  • 1,435
  • 1
  • 20
  • 38
0

Create a style element in your style.xml file. Copy the code below to your style.xml file

<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
<item name="android:windowBackground">@null</item>
<item name="android:windowIsFloating">true</item>

Then in the createDialog method of your DialogFragment class,

dialog = new Dialog(getActivity(), R.style.CustomDialog);

This is working for me and hope this will help you too

emilpmp
  • 1,716
  • 17
  • 32
-1

Try to use LayoutParams.MATCH_PARENT instead. Fill_parent is drepecated. Moreover if you have set a padding for your view that is normal that is not fill its parent's view.

Substitut
  • 373
  • 1
  • 3
  • 9