how to set margin in Dialog

WindowManager.LayoutParams:
public int x: X position... When using LEFT or START or RIGHT or END it provides an offset from the given edge
public int y: Y position... When using TOP or BOTTOM it provides an offset from the given edge
(http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#x)
thus:

final Dialog dialog = new Dialog(context);
    // ...
    // e.g. top + right margins:
    dialog.getWindow().setGravity(Gravity.TOP|Gravity.RIGHT);
    WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
    layoutParams.x = 100; // right margin
    layoutParams.y = 170; // top margin
    dialog.getWindow().setAttributes(layoutParams);
    // e.g. bottom + left margins:
    dialog.getWindow().setGravity(Gravity.BOTTOM|Gravity.LEFT);
    WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
    layoutParams.x = 100; // left margin
    layoutParams.y = 170; // bottom margin
    dialog.getWindow().setAttributes(layoutParams);

// etc.

I did a similar smiley dialog. I extend dialog

public class SmileCustomDialog extends Dialog {
Context mcontext;
GridView mGridview;

public GridView getGridview() {
    return mGridview;
}

public SmileCustomDialog(final Context context) {
    super(context, R.style.SlideFromBottomDialog);
    this.mcontext = context;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.emocategorydialog, null);
    mGridview = (GridView) v.findViewById(R.id.emogrid);
    mGridview.setSelector(new ColorDrawable(Color.TRANSPARENT));

    ImageAdapter mAdapter = new ImageAdapter(mcontext);
    mGridview.setAdapter(mAdapter);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setContentView(v);

    WindowManager.LayoutParams params = this.getWindow().getAttributes();
    this.setCanceledOnTouchOutside(true);
    params.y = -100;
    this.getWindow().setAttributes(params);

}

}

But the essential is

WindowManager.LayoutParams params = yourDialog.getWindow().getAttributes(); // change this to your dialog.

params.y = -100; // Here is the param to set your dialog position. Same with params.x
        yourDialog.getWindow().setAttributes(params);

Just add this before your show your dialog.


You can create a style for your dialog and put margins there.

For example:

<style name="custom_style_dialog"> 
    <item name="android:layout_marginStart">16dp</item>
    <item name="android:layout_marginEnd">16dp</item>
</style>

Then, in your dialog class:

class CountryDialog(
    context: Context
) : Dialog(context, R.style.custom_style_dialog) {

  //your code here
}