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 fil
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
.
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
For me, it worked when I replaced the LinearLayout parent of the layout inflated in onCreateView by RelativeLayout. No other code change required.
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)
}
}
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
}
<!-- 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.