Android get full width for custom Dialog
You need to get the current Window and set it's LayoutParams
like so:
Dialog d=new Dialog(mContext);
.........
.........
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
Alternative(if above solution does't works)
In case above code not works well you can use a workaround
styles.xml
<style name="mydialog"></style>
Java
Dialog d=new Dialog(mContext,R.style.mydialog);
.........
.........
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
Two ways this can be done, first one in style.xml and second in code:
- Add as below in style.xml, alter the value(currently 90%) to meet your needs.
<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog"> <item name="android:windowMinWidthMajor">90%</item> <item name="android:windowMinWidthMinor">90%</item> </style>
- Add setlayout to match_parent
final Dialog contacts_dialog = new Dialog(ActivityGroup.this, R.style.theme_sms_receive_dialog); contacts_dialog.setContentView(R.layout.dialog_schedule_date_time);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
contacts_dialog.setCancelable(true);
contacts_dialog.setCanceledOnTouchOutside(true);
contacts_dialog.show();
For full width dialog you can create custom style for dialog. Code is given below for full width dialog:
<style name="Custom_Dialog" parent="ThemeOverlay.AppCompat.Light" >
<item name="windowMinWidthMajor">100%</item>
<item name="windowMinWidthMinor">65%</item>
</style>
To assign this style to the dialog's constructor, add this to onCreate()
method right after setContentView()
:
getWindow()
.setLayout(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
In case anyone is wondering how to do it for a dialog shown using DialogFragment
, you can override onCreateDialog
to return a Dialog with custom style that has windowMinWidthMajor
and minor set to 90%
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.WideDialog);
}
Style:
<style name="WideDialog" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>
You don't need to add any style for that just try below one
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.your_layout_here);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.show();
Note: using empty style
is memory time consuming inside the processor. Instead directly use dailog.getWindow().setLayout(width,height)
.