How to make DialogFragment width for Fill_Parent

Hello, I am working on an android application where I use DialogFragment to display a dialog box, but its width is very small. How can I make this fill_parent width?

 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> 

enter image description here

Thanks in advance.

+68
android android-fragments android-dialogfragment fragment
Jun 02 '14 at 8:56
source share
14 answers

This is the solution that I decided to solve this problem:

 @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 the onCrateView fragment method before returning an inflated view.

 getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
+117
Oct 05
source share

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

This is the following:

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



Update:

Above the code, add the onStart() method inside onStart() .

LayoutParams.FILL_PARENT deprecated using LayoutParams.MATCH_PARENT instead.

+61
Jun 02. '14 at 9:04 on
source share

It worked for me

Create your own style:

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

Use this style in the dialog box.

 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 } 
+36
Mar 22 '16 at 8:03
source share

This worked for me when I replaced the LinearLayout parent of the layout pouted in onCreateView, RelativeLayout. No other code changes are required.

+19
Nov 24 '16 at 14:14
source share
  <!-- 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" /> 

at the top of my dialog layout did the trick.

+17
Jan 21 '16 at 1:13
source share

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 spaces between the left and right edges of the dialog and the edges of the screen on some Lollipop + devices (for example, Nexus 9).

This was not obvious, but finally, I realized that for the full width for all devices and platforms, the background of the background should be specified inside styles.xml, as shown below

 <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 a dialog as follows:

  public static DialogFragment createNoTitleDlg() { DialogFragment frag = new Some_Dialog_Frag(); frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle); return frag; } 
+10
Jul 23 '15 at 20:55
source share
 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, you can extend AppCompat_Dialog_Alert and custom attributes

+10
Mar 22 '17 at 18:30
source share

Another solution that works for me without side effects:

In your class, this extends DialogFragment:

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

In your styles:

 <style name="DialogStyle" parent="ThemeOverlay.AppCompat.Dialog.Alert"></style> 
+2
May 24 '18 at 10:56
source share

I want to clarify this. Both methods are correct , but with a 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); } 
+2
Jul 31 '18 at 7:21
source share

Create your own style in the style.xml file. Just copy this code 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> </style> 

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

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

This works for me and hope this helps too.

+1
May 4 '17 at 9:42 a.m.
source share

use this in the onCreateView method of the dialog box

  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 - the height of the fragment of the dialogue, change it as u wish

0
Mar 10 '16 at 21:55
source share

User AlertDialog.Builder inside your dialog box. Add it to the 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; } 
0
Mar 31 '16 at 7:42
source share

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> 
0
Dec 12 '17 at 11:55 on
source share

Based on other solutions, I 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) } } 
0
Mar 28 '19 at 10:11
source share



All Articles