0

I have a custom class which extends AlertDialog. I would like to create a dialog with which I can control the width, height, x, and y positions programmatically. Here is what I currently have:

MyDialog md = new MyDialog(context, AlertDialog.THEME_HOLO_LIGHT);
//md.setMessage("");
md.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(md.getWindow().getAttributes());
lp.x = 100;
lp.y = 400;
lp.width = 200;
lp.height = 400;
md.getWindow().setAttributes(lp);

If I remove the setMessage line, no dialog is displayed. If I leave it in though it will show a blank dialog that acts strangely:

  1. The height of the dialog seems locked in to the height of a single line of text rather than using the height value that is set.

  2. The dialog APPEARS to be one line tall, however, it does not allow it to go any lower on the screen than the set height would allow. A dialog positions itself on screen to ensure that all of it is always visible, which is why this occurs.

Also, if I add a lot of text to the setMessage method the dialog will stretch to contain all of it, is there a way to set it so that it crops whatever does not fit?

In summary: It appears that alert dialog does quite a bit behind the scenes in terms of formatting that I would like to disable. Is there a better way to format a dialog? The solution to this problem MUST use Dialog as a base class. Thanks in advance.

Final Hazard
  • 77
  • 1
  • 7
  • This answer might help: http://stackoverflow.com/questions/4406804/how-to-control-the-width-and-height-of-default-alert-dialog-in-android – Mike M. Jul 14 '14 at 22:56
  • I will try using an activity in a bit, but using the method alertDialog.getWindow().setLayout() has the same results. It seems as though wrap_content is overriding the height of the dialog... – Final Hazard Jul 14 '14 at 23:15
  • Yeah, I should've said "post", instead of "answer". I saw you need it to be a Dialog subclass, and meant to look at the other answers. I'll see if I can get something to work with a Dialog. But, if you're not dead-set against an Activity instead, that's definitely a simple, useable option, as I've done it myself several times. – Mike M. Jul 14 '14 at 23:17
  • 1
    I just checked activity and it looks like it will work for my purposes. I will need to rewrite a large amount of code though so if anyone has a solution to the original problem that would be much appreciated. – Final Hazard Jul 15 '14 at 00:13

0 Answers0