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:
Thank you !
I was searching an answer for this problem for some time and found it here. Thank you very much for this post.
Searching for this answer way long .Thankyou!!!
Simple and well explained solution! Thanks a lot!;)
it works but it seems to show dialog on top of screen not center
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
Kavitha, you want: if (getDialog() != null) {...your code here...}
Very much thanks for your post. That helped me a lot.
i am all ways getting dialog is null value
Thanks man! That was very helpful!
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.
Hi... what if I want to make the height of my dialog fragment 60% of the entire screen.. what should I try????
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?
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
Thank you for your explanation. It helped me so much.
Thanks so much!
Nice brother.its working
Post a Comment