Tuesday, 27 November 2012

Android: How to set the height and width of a DialogFragment

If you've played around with creating dialogs using the DialogFragment class, you've probably noticed that the layout_width and layout_height parameters that you assign in your DialogFragment's xml layout file are ignored (disrespected!) and the operating system assigns height and width to your DialogFragment however it so wishes!

Because of this problem and because it is highly recommended to create dialogs with the DialogFragment class, what I do now is specify the layout_width and layout_height parameters in my DialogFragment's xml layout file as match_parent. And then, more importantly, I specify the dialog's height and width in my DialogFragment.onStart() method as follows:
 
@Override
public void onStart() {
  super.onStart();

  // safety check
  if (getDialog() == null) {
    return;
  }

  int dialogWidth = ... // specify a value here
  int dialogHeight = ... // specify a value here

  getDialog().getWindow().setLayout(dialogWidth, dialogHeight);

  // ... other stuff you want to do in your onStart() method
}
 

18 comments:

  1. I was searching an answer for this problem for some time and found it here. Thank you very much for this post.

    ReplyDelete
  2. Searching for this answer way long .Thankyou!!!

    ReplyDelete
  3. Simple and well explained solution! Thanks a lot!;)

    ReplyDelete
  4. it works but it seems to show dialog on top of screen not center

    ReplyDelete
  5. your solution is not working for me,if i use the code like below

    ` if (getDialog() == null) {

    int dialogWidth = 1000;
    int dialogHeight = LayoutParams.WRAP_CONTENT;
    getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    }`


    How to use both?


    Thanks in advance

    ReplyDelete
  6. Kavitha, you want: if (getDialog() != null) {...your code here...}

    ReplyDelete
  7. Very much thanks for your post. That helped me a lot.

    ReplyDelete
  8. i am all ways getting dialog is null value

    ReplyDelete
  9. Thanks man! That was very helpful!

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Unfortunately this alone may not work.
    If you want to force dialog size, then also make sure that your contentview (layout for dialog) is in RelativeLayout(with match parent). Linearlayout and Framelayout(even with match parent) will not work sometimes.

    ReplyDelete
  12. Hi... what if I want to make the height of my dialog fragment 60% of the entire screen.. what should I try????

    ReplyDelete
  13. Surya, can you not get the height of the screen, work out 60% of it and then set that as the height of the dialog?

    ReplyDelete
  14. Hi Adil,

    I have another kind of question about animations.I asked on stackoverflow, but no answer, can you help me ?

    http://stackoverflow.com/questions/31130188/pop-from-fragment-back-stack-without-playing-animation

    ReplyDelete
  15. Thank you for your explanation. It helped me so much.

    ReplyDelete