How to make DialogFragment width to Fill_Parent
Asked Answered
H

21

118

I am working on an android application where I am using DialogFragment to display the dialog but its width is very small. How I can make this width to fill_parent to it ?

public class AddNoteDialogFragment extends DialogFragment {

    public AddNoteDialogFragment() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getDialog().setTitle(getString(R.string.app_name));
        View view = inflater.inflate(R.layout.fragment_add_note_dialog,
                container);

        return view;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }
}

fragment_add_note_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <EditText
        android:id="@+id/addNoteEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:hint="@string/clock_enter_add_note"
        android:imeOptions="actionDone"
        android:inputType="textCapSentences|textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/login_button"
        android:text="@string/submit_button"
        android:textColor="@android:color/white" />

</LinearLayout>

Halliehallman answered 2/6, 2014 at 8:56 Comment(2)
Actually, the exact same layout using RelativeLayout is displaying very well. I don't explain that though...Crosier
Try this answer - Kotlin(2022) ---------- https://mcmap.net/q/188889/-how-to-set-the-width-of-a-dialogfragment-in-percentageWeatherwise
B
180

This is the solution I figured out to handle this issue:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);    
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    return dialog;
}

@Override
public void onStart() {
    super.onStart();

    Dialog dialog = getDialog();
    if (dialog != null) {
       dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
       dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }
}

Edit:

You can use the code below in onCrateView method of Fragment before return the inflated view.

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Bolyard answered 5/10, 2014 at 21:44 Comment(4)
You can just call dialog.getWindow().setBackgroundDrawable(null);Revers
if it's setted to null, it doesn't be transparent... only black, on 4.0.4Pennon
For me, it works if I put getDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); in the DialogFragment's onResume() method. It did not work on the onCreateView() method. I modified the answer slightly from dialog to getDialog().Medarda
setLayout in the onStart method works perfectly. Thanks.Directory
J
98

Have you tried using the answer of Elvis from How to make an alert dialog fill 90% of screen size?

It is the following:

dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

Update:

Above code should be added inside onStart() method of the DialogFragment.

Jainism answered 2/6, 2014 at 9:4 Comment(7)
Which LayoutParams did you import?Unparalleled
@Unparalleled Very good question actually. Try WindowManager.LayoutParams.FILL_PARENT.Jainism
@Unparalleled although technically, they are all just inheriting this value from ViewGroup.LayoutParams, so it works perfectly fine either way.Jainism
@Jainism Ah okay, I imported the first suggestion and it works :D Thank you!Unparalleled
As mentioned here https://mcmap.net/q/92335/-how-to-create-a-dialogfragment-without-title I would suggest moving this code to onCreateDialogBrassica
import android.view.WindowManager;Edythedythe
This single line does the trick!! Nice!Petrozavodsk
K
68

This is worked for me

Create your custom style :

   <style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
    <item name="android:windowMinWidthMajor">97%</item>
    <item name="android:windowMinWidthMinor">97%</item>
   </style>

You can also try use the right parent to match your other dialogs. for example parent="ThemeOverlay.AppCompat.Dialog" and so on.

Use this style in your dialog

public class MessageDialog extends DialogFragment {

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
}

// your code 
}
Klemm answered 22/3, 2016 at 8:3 Comment(2)
beautiful solution with <item name="android:windowBackground">@null</item>.Adjuvant
take care to use the right parent to match your other dialogs. on my case it was ThemeOverlay.AppCompat.DialogNicky
I
40

For me, it worked when I replaced the LinearLayout parent of the layout inflated in onCreateView by RelativeLayout. No other code change required.

Incinerator answered 24/11, 2016 at 14:14 Comment(2)
Typical, always the most unpopular answer on stack overflow solves my problem. ThanksVenita
I liked this answer very much but don't the reason why it works for relative layout only?Puissance
M
22
    <!-- A blank view to force the layout to be full-width -->
    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_gravity="fill_horizontal" />

in the top of my dialog layout did the trick.

Mildew answered 21/1, 2016 at 1:13 Comment(4)
This works for me, and I like that it doesn't require us to add anything extra in code, but I'm wondering why it works...Chemotaxis
Incredibly, this is the only thing that worked for me. Custom style did not.Obligor
perfect. need something simple like this.Perron
What was your root layout?Susy
J
20
public class FullWidthDialogFragment extends AppCompatDialogFragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat_Light_Dialog_Alert);
    }
}

and if you want much more flexibility can extend AppCompat_Dialog_Alert and custom attributes

Justis answered 22/3, 2017 at 18:30 Comment(0)
B
18

Based on other solutions, I have created my own.

<style name="AppTheme.Dialog.Custom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowMinWidthMajor">100%</item>
    <item name="android:windowMinWidthMinor">100%</item>
</style>
abstract class BaseDialogFragment : DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AppTheme_Dialog_Custom)
    }
}
Birdiebirdlike answered 28/3, 2019 at 10:11 Comment(0)
V
12

In my case I also used the following approach:

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.dialog_height));
    }
}

But there were still small gaps between left and right edges of the dialog and the screen edges on some Lollipop+ devices (e.g. Nexus 9).

It was not obvious but finally I figured out that to make it full width across all the devices and platforms window background should be specified inside styles.xml like the following:

<style name="Dialog.NoTitle" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:padding">0dp</item>
    <item name="android:windowBackground">@color/window_bg</item>
</style>

And of course this style needs to be used when we create the dialog like the following:

    public static DialogFragment createNoTitleDlg() {
        DialogFragment frag = new Some_Dialog_Frag();
        frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle);
        return frag;
}
Venuti answered 23/7, 2015 at 20:55 Comment(1)
This should be the accepted answer! Saved me so much of time! The window background did the trick.Chordate
H
12

I want clarify this. Both the ways are right, But with different DialogFragment.

Using android.app.DialogFragment

@Override
public void onStart()
{
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
}

Using android.support.v4.app.DialogFragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
Hornet answered 31/7, 2018 at 7:21 Comment(1)
Basically I needed full width and this worked. Thanks!Nels
W
4

This works for me perfectly.

android:minWidth="300dp"

Through this you can give the width to dialog Fragment.

Wharve answered 10/3, 2020 at 2:19 Comment(0)
F
2

User AlertDialog.Builder inside your DialogFragment. Add it in onCreateDialog method like this. And in onCreateView do nothing.

public Dialog onCreateDialog(Bundle savedInstanceState)
{

    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    View view = LayoutInflater.from(getContext()).inflate(R.layout.gf_confirm_order_timeout_dialog, null);
    final Bundle args = getArguments();
    String message = args.getString(GfConstant.EXTRA_DATA);
    TextView txtMessage = (TextView) view.findViewById(R.id.txt_message);
    txtMessage.setText(message);

    view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (mListener != null)
            {
                mListener.onDialogConfirmOK();
            }
        }
    });
    builder.setView(view);
    Dialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(false);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}
Frontolysis answered 31/3, 2016 at 7:42 Comment(0)
S
2

Create a custom style in your style.xml file. Just copy paste this code into 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>
</style>

Then in the createDialog method of your DialogFragment, create the dialog object by the code

 dialog = new Dialog(getActivity(), R.style.CustomDialog);
Siccative answered 4/5, 2017 at 9:42 Comment(1)
I had to add the following to the style to make it work for me: <item name="android:windowMinWidthMajor">100%</item> <item name="android:windowMinWidthMinor">100%</item>Flounder
P
2

This is how i solved when i faced this issue. Try this.

in your DialogFragment's onActivityCreated, Add this code according to your need....

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        myview=inflater.inflate(R.layout.fragment_insurance_detail, container, false);
        intitviews();
        return myview;
    }
    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        getDialog().getWindow().setGravity(Gravity.CENTER);
    }
Pronator answered 22/7, 2019 at 13:12 Comment(0)
H
2

option 1. add following code in oncreate in activity

 getDialog().getWindow()
         .setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

or you can Create a custom style for Dialog

<style name="CustomDialog" parent="AppTheme" >
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item>
  <item name="android:windowIsFloating">true</item>
  <item name="android:windowCloseOnTouchOutside">true</item>
</style>


 then use that style in dialog fragment


@Override public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
}

@Override public void onStart() {
  super.onStart();
  getDialog().getWindow()
    .setLayout(WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT);
}

SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");

OR you try the following code in style will help you

<item name="android:windowBackground">@null</item>

Solution 2: dialog.window.setLayout

class TestDialog : DialogFragment() {

    override fun onStart() {
        super.onStart()

        dialog?.window?.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
    }

}
Hassock answered 15/10, 2020 at 11:41 Comment(1)
Only solution 2 is working (dialog.window.setLayout)Vernellvernen
I
2

I end up with a decent solution for this issue.

Based in @Владислав-Стариков answer.

First you need to create Theme overlay where AppTheme is your main theme so it will apply the same attributes in your main theme like the following:

  <style name="AppTheme.DialogOverlay" parent="ThemeOverlay.AppCompat.Dialog.Alert">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowMinWidthMajor">50%</item>
    <item name="android:windowMinWidthMinor">90%</item>
  </style>

Then override onCreate in your DialogFragment class

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(STYLE_NORMAL, R.style.AppTheme_DialogOverlay)
  }
Inelegance answered 22/3, 2022 at 15:2 Comment(1)
Good answer. You can also override getTheme() and pass the theme id to get the same result, instead of overridingonCreate() and using setStyle(). override fun getTheme() = R.style.AppTheme_DialogOverlayLabium
B
1

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

220 is dialog fragment height, change it as u wish

Baun answered 10/3, 2016 at 21:55 Comment(0)
G
1

in your layout root, set android:minWidth to a very large value e.g

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

   ...

</LinearLayout>
Gan answered 12/12, 2017 at 11:55 Comment(0)
W
0

I tried multiple answers listed above; they didn't work for me. This is the solution to my issue:

In DialogFragment(), add the codes below:

override fun onResume() {
    super.onResume()
    if (showsDialog) {
        val width = ViewGroup.LayoutParams.MATCH_PARENT
        val height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.apply {
            setBackgroundDrawable(ColorDrawable(Color.WHITE))
            attributes.gravity = Gravity.BOTTOM
            setLayout(width, height)
        }
    }
}
Was answered 4/6, 2020 at 4:10 Comment(0)
C
0

A clearer and more flexible approach is to set a percentage of the screen's width. Can do the same for the height and it's easy to change.

override fun onResume() {
    super.onResume()
    dialog?.window?.let { window ->
        val params: ViewGroup.LayoutParams = window.attributes
        params.width = context?.getScreenSize(false)?.x?.times(0.9)?.toInt()
            ?: ViewGroup.LayoutParams.MATCH_PARENT
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT
        window.attributes = params as WindowManager.LayoutParams
    }
}
Clementeclementi answered 12/4, 2021 at 2:23 Comment(0)
R
0

This worked for me,

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Dialog_NoActionBar_MinWidth);
}
Rhaetian answered 14/8, 2023 at 0:24 Comment(0)
S
-2

It goes full width, if ConstraintLayout is used as a root layout, without any additional code.

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</androidx.constraintlayout.widget.ConstraintLayout>
Shortie answered 6/12, 2020 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.