20

I am trying to create a DialogFragment with a width of MATCH_PARENT so the dialog is nearly full screen (leaving the padding around the edges for the floating look). I have seen this solution Full Screen DialogFragment in Android but am trying to avoid the hack of setting the width to 1000dp. With my current layout, whether I use FILL_PARENT or MATCH_PARENT it seems to be setting the width and height to WRAP_CONTENT.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

I have used this solution for Dialog (not DialogFragment) and it works as expected:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Community
  • 1
  • 1
invertigo
  • 6,336
  • 5
  • 39
  • 64
  • 2
    I've added android:minWidth="1000dp" to the above layout and it is giving me the desired results, but I'm still looking for a more elegant solution (if there is one). – invertigo Aug 09 '12 at 16:28

10 Answers10

38

this is perfect for me. when extends DialogFragment override onCreateView()... impelments all logic

dialog make it Full Screen just override this method

 @Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {

    // the content
    final RelativeLayout root = new RelativeLayout(getActivity());
    root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    // creating the fullscreen dialog
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(root);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    return dialog;
}
Attif
  • 1,158
  • 13
  • 17
  • good but how can remove status bar also? I want a full full screen just my layout! – Amir Hossein Ghasemi Apr 06 '15 at 13:34
  • 1
    JUST add this line code to be fullscreen without statusbar : `dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);` – Amir Hossein Ghasemi Apr 06 '15 at 13:38
  • On `onCreateView()` of your DialogFragment add following `getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITL);` Works on me – Sattar Hummatli Jul 13 '16 at 13:36
  • A source is http://andraskindler.com/blog/2013/creating-a-fullscreen-dialogfragment-with-a-custom-background/. Instead of Color.YELLOW you may write Color.TRANSPARENT. It is required line, if you remove it, there will appear borders around a dialog. – CoolMind Aug 22 '16 at 09:50
  • @AmirHosseinGhasemi, adding `getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);` to any part of `onCreateView()` leads to exception: `requestFeature() must be called before adding content`. – CoolMind Aug 22 '16 at 09:56
  • If you want to set margins see this SO answer: http://stackoverflow.com/a/42419832/2011622 – Peter F Feb 24 '17 at 10:56
21

try to switch to RelativeLayout instead of LinearLayout. It worked in my case

atermenji
  • 1,338
  • 8
  • 19
8
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE,android.R.style.Theme_Holo_Light);
}
evya
  • 3,381
  • 1
  • 25
  • 28
3

this answer help me How to set DialogFragment's width and height?

int width = activity.getResources().getDisplayMetrics().widthPixels;
int height = activity.getResources().getDisplayMetrics().heightPixels;
content.setLayoutParams(new LinearLayout.LayoutParams(width, height));
Community
  • 1
  • 1
Danylo.Vus
  • 969
  • 9
  • 11
2

The best solution I found was to set the width or minWidth to some arbitrarily large value to ensure it fills the screen. I don't like this solution, but I haven't seen anything better.

invertigo
  • 6,336
  • 5
  • 39
  • 64
2

In my code, I was wanting a fullscreen dialog, but still show the notification bar at the top of the screen, so I'm using:

setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);

This makes it fullscreen, but I'm not using a fullscreen style so that I keep the notification bar.

Hope it helps someone!

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28
1

You can also do something like-

@Override
  public void onCreate(@Nullable final Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      setStyle(STYLE_NO_TITLE, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
    } else {
      setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar);
    }
    super.onCreate(savedInstanceState);

  }

By this you will also be able to see the status bar colored for versions >= lollipop

Vaibhav Sharma
  • 2,293
  • 1
  • 17
  • 24
  • Right answer! But not need check Build.Version, simple use you default AppTheme. – maXp Aug 03 '17 at 22:18
0

On DialogFragment following works. In onCreateView() add following

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //…
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    //…
    return view;
}
Sattar Hummatli
  • 1,360
  • 1
  • 15
  • 26
0

I have answered this question with explanation this is the shortest and efficient way. Please check In this thread

Hope this helps :)

Biswajit
  • 1,829
  • 1
  • 18
  • 33
-1

To have a full screen dialog, you need use

public class CustomDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        return inflater.inflate(R.layout.purchase_items, container, false);
    }


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

And to show this dialog:

CustomDialogFragment newFragment = new CustomDialogFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
fragmentManager.beginTransaction().add(android.R.id.content, newFragment).commit();

ATTENTION: You can't use following code to show the dialog, if not, the dialog will be shown in center of screen, not full screen.

//don't show dialog like this
newFragment.show(fragmentManager, "dialog");
loic
  • 107
  • 1
  • 5