I am looking for a way to get size of a custom dialog. I went through this question, but the only answer given is pretty useless, because if I try mDialog.getWindow().getAttributes().height;
it only returns -2, which is a constant for WRAP_CONTENT
attribute which I set to dialog. How can I get the size of it. I want to know the siye for the background image.
Asked
Active
Viewed 1.9k times
15
-
what dialog exactly do you use? – Korniltsev Anatoly Nov 22 '12 at 15:14
-
public class ControlOverlay extends Dialog, so just Dialog – slezadav Nov 22 '12 at 15:29
4 Answers
22
Give it a try after the dialog will be showed:
mDialog.getWindow().getDecorView().getHeight()

antaki93
- 704
- 7
- 10

Korniltsev Anatoly
- 3,676
- 2
- 26
- 37
-
1Great works really well, Michal's answer is better for my case though. – slezadav Nov 22 '12 at 15:32
-
12
-
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); //call here } – Pawan Chaurasiya May 13 '16 at 15:19
-
13
Actually, in Android it doesn't work like in iOS - you can't get the size of the View
itself, what you can do, though, is to ask for the size of the ROOT layout of that view.
e.g.:
myDialog.this.findViewById(R.id.dialog_root_layout).getHeight());

Michal
- 15,429
- 10
- 73
- 104
-
2Although it returns the size of the dialog without a header, it is exactly what I needed. – slezadav Nov 22 '12 at 15:32
-
@Michal: I get `dialog_root_layout cannot be resolved or is not a field` for an `AlertDialog`. – Luis A. Florit Dec 25 '13 at 17:10
-
@LuisA.Florit Instead of putting dialog_root_layout you should insert ID of your dialog. – Michal Jan 10 '14 at 10:40
-
@Michal: Ooops, now I understand what you meant. However, I had some problems with your suggestion: First, I get an error `myDialog cannot be resolved to a type`. Second, myDialog is created with a `final AlertDialog myDialog = builder.create()`, and there is no `setID` method for an `AlertDialog`, so how do I get its root layout ID? Thanks! – Luis A. Florit Jan 10 '14 at 17:22
7
@Kormilsev Anatoliy has answered correct and I am just improving. So in the class you inherit from Dialog class just override the method:
@Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
height = getWindow().getDecorView().getHeight();
}

Vadim Kotov
- 8,084
- 8
- 48
- 62

CodeToLife
- 3,672
- 2
- 41
- 29
-
-
3this `alertDialog.setOnShowListener(new DialogInterface.OnShowListener()` will work – user924 Dec 09 '17 at 18:38
-
-
1
-
5
In case, if you have your own XML layouts for custom dialog.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dialog_main_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/primaryBackground">
/* whatever you want here */
</android.support.constraint.ConstraintLayout>
In activity:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.popup_gameover);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface d) {
View view = dialog.findViewById(R.id.dialog_main_layout);
int width = view.getWidth();
int height = view.getHeight();
...
}
});
This width
and height
exacly as expected.

Alexey Ishkov
- 399
- 4
- 4