0

I have created a Custom AlertDialog like this -

remark_builder = new AlertDialog.Builder(this);
        // Get the layout inflater
        LayoutInflater inflater = this.getLayoutInflater();
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the
        // dialog layout
        remark_builder.setCancelable(true);
        final View dialogView = inflater.inflate(R.layout.add_remark_dialog, null);
        remark_builder.setView(dialogView);

if (remark_dialog == null || !remark_dialog.isShowing()) {

            remark_dialog = remark_builder.create();
            remark_dialog.setCanceledOnTouchOutside(false);
            remark_dialog.show();

        }

I want to add margins to the left and right of this alertdialog.

I have tried a lot of code over the internet but it is emphasizing on setting the width and height of the Alert Dialog. I don't want to set the width and height. I want to add margins to the left and right of the AlertDialog which is MATCH_PARENT.

My current alert dialog looks like :

enter image description here

I want to add margins like 40 or 50 or according to the device density. is this possible?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Aman Verma
  • 3,155
  • 7
  • 30
  • 60

1 Answers1

0

You can try the below method to achieve this.

     Rect displayRectangle = new Rect();
            Window window = mContext.getWindow();

            window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
remark_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        malertdialog.show();
        malertdialog.getWindow().setLayout((int)(displayRectangle.width() *
                0.8f), (int)(displayRectangle.height() / 2));

adjust the decimal value for the width and height as per what ratio you need.

abhil nair
  • 188
  • 1
  • 1
  • 15
  • Your code is working but there is one problem. The dialog is not in the center. I tried setting the gravity using this - remark_dialog.getWindow().setGravity(Gravity.CENTER); but it is not working. Any suggestions?? – Aman Verma Nov 22 '18 at 16:22
  • my remark dialog layout is wrap_content. How to set the dialog to the center gravity? – Aman Verma Nov 22 '18 at 17:36