Android Getting Calculated View Dimensions edit

Calculating initial View dimensions in an Activity

package com.example;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;

public class ExampleActivity extends Activity {

    protected void onCreate(@Nullable final Bundle savedInstanceState) {

        final View viewToMeasure = findViewById(;

        // viewToMeasure dimensions are not known at this point.
        // viewToMeasure.getWidth() and viewToMeasure.getHeight() both return 0,
        // regardless of on-screen size.

        viewToMeasure.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                // viewToMeasure is now measured and laid out, and displayed dimensions are known.
                logComputedViewDimensions(viewToMeasure.getWidth(), viewToMeasure.getHeight());
                // Remove this listener, as we have now successfully calculated the desired dimensions.

                // Always return true to continue drawing.
                return true; 

    private void logComputedViewDimensions(final int width, final int height) {
        Log.d("example", "viewToMeasure has width " + width);
        Log.d("example", "viewToMeasure has height " + height);


Feedback about page:

Optional: your email if you want me to get back to you:

Getting Calculated View Dimensions/
Getting Calculated View Dimensions
Calculating initial View dimensions in an Activity

Table Of Contents
12 Getting Calculated View Dimensions
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close