How can I get a width/height of Dialog with FILL_PARENT layout?

I have an activity with

android:theme="@android:style/Theme.Dialog" 

to show it as float window. Also I have a line

getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

And I need a width/height of this window to show content correctly.

I've tested four options and did not get an answer:

//1 
int width = getWindowManager().getDefaultDisplay().getWidth();

//2 
int width = getWindow().getDecorView().getWidth();

//3 
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

//4 
int width = getWindow().getAttributes().width;

Thanks!

Upvotes: 1

Views: 3874

Answers (1)

Boe-Dev
Boe-Dev

Reputation: 1595

use this function:

private int getting_screen_width(Dialog dialog) 
    { 
    DisplayMetrics dm = new DisplayMetrics();
     dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
    String width = ""+dm.widthPixels ;
    dm = null;
       return (Integer.parseInt(width));

this return the value of the screen width

Upvotes: 3

Related Questions