Does anybody knows how can I set margins to a custom dialog? I\'m asking because I\'ve a custom dialog but when displayed it stretches to fill the parent, even though I set
1) change your main parent layout height match_content
like below :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_content"
android:layout_margin="50dip"
android:orientation="vertical"
android:layout_gravity="top|center">
2) now add style to your styles.xml
<style name="dialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert"/>
3) now add below code to show dialog :
Dialog dialog = new Dialog(DayActivity.this, R.style.dialogTheme);
dialog.setContentView(R.layout.dialog_custom_layout);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
dialog.show();
I had the same problem and I solved it by setting the window background on an InsetDrawable:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
...
...
AlertDialog dialog = builder.create();
ColorDrawable back = new ColorDrawable(Color.TRANSPARENT);
InsetDrawable inset = new InsetDrawable(back, 20);
dialog.getWindow().setBackgroundDrawable(inset);
dialog.show();
In this case the dialog will appear with a margin of 20 to all edges.
One easy way is to set the root layout to transparent background. Then, have its first child have margins or pre set height and width.