android – Full screen DialogFragment-ThrowExceptions

Exception or error:

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);
How to solve:

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;
}

###

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

###

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE,android.R.style.Theme_Holo_Light);
}

###

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

###

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.

###

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!

###

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

###

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

Hope this helps 🙂

###

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;
}

###

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");

Leave a Reply

Your email address will not be published. Required fields are marked *