How to retrieve the dimensions of a view? How to retrieve the dimensions of a view? android android

How to retrieve the dimensions of a view?


I believe the OP is long gone, but in case this answer is able to help future searchers, I thought I'd post a solution that I have found. I have added this code into my onCreate() method:

EDITED: 07/05/11 to include code from comments:

final TextView tv = (TextView)findViewById(R.id.image_test);ViewTreeObserver vto = tv.getViewTreeObserver();vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {    @Override    public void onGlobalLayout() {        LayerDrawable ld = (LayerDrawable)tv.getBackground();        ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0);        ViewTreeObserver obs = tv.getViewTreeObserver();        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {            obs.removeOnGlobalLayoutListener(this);        } else {            obs.removeGlobalOnLayoutListener(this);        }    }});

First I get a final reference to my TextView (to access in the onGlobalLayout() method). Next, I get the ViewTreeObserver from my TextView, and add an OnGlobalLayoutListener, overriding onGLobalLayout (there does not seem to be a superclass method to invoke here...) and adding my code which requires knowing the measurements of the view into this listener. All works as expected for me, so I hope that this is able to help.


I'll just add an alternative solution, override your activity's onWindowFocusChanged method and you will be able to get the values of getHeight(), getWidth() from there.

@Overridepublic void onWindowFocusChanged (boolean hasFocus) {        // the height will be set at this point        int height = myEverySoTallView.getMeasuredHeight(); }


You are trying to get width and height of an elements, that weren't drawn yet.

If you use debug and stop at some point, you'll see, that your device screen is still empty, that's because your elements weren't drawn yet, so you can't get width and height of something, that doesn't yet exist.

And, I might be wrong, but setWidth() is not always respected, Layout lays out it's children and decides how to measure them (calling child.measure()), so If you set setWidth(), you are not guaranteed to get this width after element will be drawn.

What you need, is to use getMeasuredWidth() (the most recent measure of your View) somewhere after the view was actually drawn.

Look into Activity lifecycle for finding the best moment.

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

I believe a good practice is to use OnGlobalLayoutListener like this:

yourView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {        @Override        public void onGlobalLayout() {            if (!mMeasured) {                // Here your view is already layed out and measured for the first time                mMeasured = true; // Some optional flag to mark, that we already got the sizes            }        }    });

You can place this code directly in onCreate(), and it will be invoked when views will be laid out.