android – Get content view size in onCreate-ThrowExceptions

Exception or error:

I’m looking for a good way to measure the dimensions of the actual content area for an activity in Android.

Getting display always works. Simply go like this:

Display display = getWindowManager().getDefaultDisplay();

And you can get the pixel count for the entire screen. Of course this does not take into consideration the ActionBar, status bar, or any other views which will reduce the available size of the activity itself.

Once the activity is running, you can do this:

View content = getWindow().findViewById(Window.ID_ANDROID_CONTENT);

To get the activity content only. But doing this in onCreate() will result in a view with width and height of 0, 0.

Is there a way to get these dimensions during onCreate? I imagine there ought to be a way to get the measurements of any status bars and just subtract that from the total display size, but I’m unable to find a way to do that. I think this would be the only way, because the content window method will always return a view with no width/height before it is drawn.

Thanks!

How to solve:

You can use a layout or pre-draw listener for this, depending on your goals. For example, in onCreate():

final View content = findViewById(android.R.id.content);
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        //Remove it here unless you want to get this callback for EVERY
        //layout pass, which can get you into infinite loops if you ever
        //modify the layout from within this method.
        content.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        //Now you can get the width and height from content
    }
});

Update
as of API 16 removeGlobalOnLayoutListener is deprecated.

Change to:
content.getViewTreeObserver().removeOnGlobalLayoutListener(this)

###

(copied from my answer to a related question)

I use the following technique – post a runnable from onCreate() that will be executed when the view has been created:

    contentView = findViewById(android.R.id.content);
    contentView.post(new Runnable()
    {
        public void run()
        {
            contentHeight = contentView.getHeight();
        }
    });

This code will run on the main UI thread, after onCreate() has finished.

###

This appears to be a duplicate question. There is an elegant answer within this SO question:

getWidth() and getHeight() of View returns 0

Which (shamelessly copied) is to override onWindowFocusChanged(), which seems to fire just after onCreate(), and where the sizes are rendered:

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  //Here you can get the size!
 }

###

Answer with post is incorrect, because the size might not be recalculated.
Another important thing is that the view and all it ancestors must be visible. For that I use a property View.isShown.

Here is my kotlin function, that can be placed somewhere in utils:

fun View.onInitialized(onInit: () -> Unit) {
    viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            if (isShown) {
                viewTreeObserver.removeOnGlobalLayoutListener(this)
                onInit()
            }
        }
    })
}

And the usage is:

myView.onInitialized {
    Log.d(TAG, "width is: " + myView.width)
}

###

If you want lo load a Bitmap into a ImageView within OnCreate(), you can use this example to do it:

public static void setImageLater(@NonNull final ImageView imageView,final Bitmap bitmap){
    final ViewTreeObserver observer = imageView.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    imageView.setImageBitmap(bitmap);
                }
            });
}

###

You can do any work wich needs sizes in onResume(using flag like alreadyDone not to repeat it every time an Activity goes foreground). In onCreate views are not displayed, so it’s normal that sizes are zeros.

Leave a Reply

Your email address will not be published. Required fields are marked *