How to make DialogFragment width to Fill_Parent
Solution 1:
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));
Solution 2:
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
.
Solution 3:
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
}
Solution 4:
For me, it worked when I replaced the LinearLayout parent of the layout inflated in onCreateView by RelativeLayout. No other code change required.
Solution 5:
<!-- 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.