How to add margins to Left and Right to Custom AlertDialog?












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?










share|improve this question
























  • You want to make it smaller?
    – Skizo-ozᴉʞS
    Nov 11 at 19:21










  • Take a look : stackoverflow.com/a/7655350/4385913
    – Skizo-ozᴉʞS
    Nov 11 at 19:22










  • Yeah, width. Are your sure the provided link will work??
    – Sniper
    Nov 11 at 20:00










  • Binary XML file line #2: Error inflating class <unknown> getting error.
    – Sniper
    Nov 11 at 20:12










  • please provide xml layout of dialog
    – Touhidul Islam
    Nov 12 at 7:19


















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?










share|improve this question
























  • You want to make it smaller?
    – Skizo-ozᴉʞS
    Nov 11 at 19:21










  • Take a look : stackoverflow.com/a/7655350/4385913
    – Skizo-ozᴉʞS
    Nov 11 at 19:22










  • Yeah, width. Are your sure the provided link will work??
    – Sniper
    Nov 11 at 20:00










  • Binary XML file line #2: Error inflating class <unknown> getting error.
    – Sniper
    Nov 11 at 20:12










  • please provide xml layout of dialog
    – Touhidul Islam
    Nov 12 at 7:19
















0












0








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?










share|improve this question















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?







android android-layout alertdialog android-alertdialog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 19:18









Fantômas

32.3k156288




32.3k156288










asked Nov 11 at 18:42









Sniper

82221129




82221129












  • You want to make it smaller?
    – Skizo-ozᴉʞS
    Nov 11 at 19:21










  • Take a look : stackoverflow.com/a/7655350/4385913
    – Skizo-ozᴉʞS
    Nov 11 at 19:22










  • Yeah, width. Are your sure the provided link will work??
    – Sniper
    Nov 11 at 20:00










  • Binary XML file line #2: Error inflating class <unknown> getting error.
    – Sniper
    Nov 11 at 20:12










  • please provide xml layout of dialog
    – Touhidul Islam
    Nov 12 at 7:19




















  • You want to make it smaller?
    – Skizo-ozᴉʞS
    Nov 11 at 19:21










  • Take a look : stackoverflow.com/a/7655350/4385913
    – Skizo-ozᴉʞS
    Nov 11 at 19:22










  • Yeah, width. Are your sure the provided link will work??
    – Sniper
    Nov 11 at 20:00










  • Binary XML file line #2: Error inflating class <unknown> getting error.
    – Sniper
    Nov 11 at 20:12










  • please provide xml layout of dialog
    – Touhidul Islam
    Nov 12 at 7:19


















You want to make it smaller?
– Skizo-ozᴉʞS
Nov 11 at 19:21




You want to make it smaller?
– Skizo-ozᴉʞS
Nov 11 at 19:21












Take a look : stackoverflow.com/a/7655350/4385913
– Skizo-ozᴉʞS
Nov 11 at 19:22




Take a look : stackoverflow.com/a/7655350/4385913
– Skizo-ozᴉʞS
Nov 11 at 19:22












Yeah, width. Are your sure the provided link will work??
– Sniper
Nov 11 at 20:00




Yeah, width. Are your sure the provided link will work??
– Sniper
Nov 11 at 20:00












Binary XML file line #2: Error inflating class <unknown> getting error.
– Sniper
Nov 11 at 20:12




Binary XML file line #2: Error inflating class <unknown> getting error.
– Sniper
Nov 11 at 20:12












please provide xml layout of dialog
– Touhidul Islam
Nov 12 at 7:19






please provide xml layout of dialog
– Touhidul Islam
Nov 12 at 7:19














1 Answer
1






active

oldest

votes


















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.






share|improve this answer





















  • 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??
    – Sniper
    Nov 22 at 16:22










  • my remark dialog layout is wrap_content. How to set the dialog to the center gravity?
    – Sniper
    Nov 22 at 17:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251969%2fhow-to-add-margins-to-left-and-right-to-custom-alertdialog%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer





















  • 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??
    – Sniper
    Nov 22 at 16:22










  • my remark dialog layout is wrap_content. How to set the dialog to the center gravity?
    – Sniper
    Nov 22 at 17:36
















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.






share|improve this answer





















  • 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??
    – Sniper
    Nov 22 at 16:22










  • my remark dialog layout is wrap_content. How to set the dialog to the center gravity?
    – Sniper
    Nov 22 at 17:36














0












0








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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 6:39









abhil nair

128113




128113












  • 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??
    – Sniper
    Nov 22 at 16:22










  • my remark dialog layout is wrap_content. How to set the dialog to the center gravity?
    – Sniper
    Nov 22 at 17:36


















  • 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??
    – Sniper
    Nov 22 at 16:22










  • my remark dialog layout is wrap_content. How to set the dialog to the center gravity?
    – Sniper
    Nov 22 at 17:36
















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??
– Sniper
Nov 22 at 16:22




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??
– Sniper
Nov 22 at 16:22












my remark dialog layout is wrap_content. How to set the dialog to the center gravity?
– Sniper
Nov 22 at 17:36




my remark dialog layout is wrap_content. How to set the dialog to the center gravity?
– Sniper
Nov 22 at 17:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251969%2fhow-to-add-margins-to-left-and-right-to-custom-alertdialog%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation