Waypoint
Waypoint

Reputation: 17753

Android: get dimensions of activity window

in my certain activity, I am using theme -

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

Is there any way how to find out, actual dimensions of this windows on the screen? My last efforts ended in:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();

which is giving me actual resolution of the whole screen. Any ideas?

Thx

Upvotes: 2

Views: 8641

Answers (4)

Shahar
Shahar

Reputation: 3692

there is a very easy way

    <RelativeLayout mlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rootView"
        >
... some other views
    </RelativeLayout>

and than, in your activity, you need to get the view once its drawn.

like this:

private View mRoot;

    @Override
    protected void onResume() {
        super.onResume();

        mRoot = findViewById(R.id.rootView);

        /* after the main view is loaded, start positioning the views */
        mRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
// in here, the view is already loaded, you can get his size.
// without the navigation / status / title bar
                // remove the observer, we don't need it anymore
mRoot.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mButton.setRadius(1000);
                mButton.setMinimumWidth(mRoot.getWidth() / 2);
                mButton.setY(mRoot.getHeight() / 3 * 2);
            }
        });
    }

hope that helps.

Upvotes: 0

cyrilchampier
cyrilchampier

Reputation: 2248

From API level 13: Point screenSize = new Point(); getActivity().getWindowManager().getDefaultDisplay().getSize(screenSize);

see here: http://developer.android.com/reference/android/view/Display.html#getSize(android.graphics.Point)

Upvotes: 3

Android2390
Android2390

Reputation: 1150

There are two methods you can do : the first one is like this :

           Display display = getActivity().getWindowManager().getDefaultDisplay(); 
           int width = display.getWidth();
           int widthdp = (int) (width / getResources().getDisplayMetrics().density);
           int height = display.getHeight();
           int heightdp = (int) (height / getResources().getDisplayMetrics().density);

Second one you can use is this :

                Point size = new Point();
                display.getSize(size);
                int width_one = size.x;
                int height_one = size.y;

Upvotes: -1

Pavel Dudka
Pavel Dudka

Reputation: 20934

Not sure this is fully correct solution, but I resolved this problem by getting the root view from layout you are inflating for your dialog (you need to assign the id in this case) and after that it is pretty straight forward to get dimensions:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginTop="103dp"
    android:background="#FFFFFF">

    <RelativeLayout 
        android:id="@+id/report_header"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:paddingLeft="15dp">
        ....
    </RelativeLayout>
</RelativeLayout>

In your dialog activity you need to get your root:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.dialog_layout);
    View root = findViewById(R.id.root);
}

And after that you can get dialog dimensions by:

Rect r = new Rect ( 0, 0, 0, 0 );
root.getHitRect ( r );

After this call, r will contain dimensions you are looking for

Upvotes: 0

Related Questions