25

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!

JMRboosties
  • 15,500
  • 20
  • 76
  • 116
  • 1
    check this out, this might help http://stackoverflow.com/questions/15074578/creating-a-scaled-bitmap-in-oncreate-according-to-the-imageviews-height-and-wi – JRowan Sep 17 '13 at 23:40
  • 1
    The window may not have drawn in onCreate and thats the reason why you get 0,0. I suggest you to move the code to onStart so that atleast the view is drawn. – prijupaul Sep 17 '13 at 23:41
  • 1
    I tried to get the content in create, start, and resume as a test and all 3 returned 0, 0. It was worth a shot though. – JMRboosties Sep 17 '13 at 23:56

6 Answers6

34

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)

IAbstract
  • 19,551
  • 15
  • 98
  • 146
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 1
    This works, thanks. I just wish there was a way to do this without listeners, but whatever. – JMRboosties Sep 18 '13 at 20:32
  • 1
    You probably can. Depends on what you intend to do with this information. If the content view itself needs to know about its size, make a custom ViewGroup, and override `onLayout`. At that point, you'll know your width and height. – Kevin Coppock Sep 18 '13 at 20:40
  • 1
    @blabus it's actually not, it's an API typo. They did add a corrected version in API 16, though. – Kevin Coppock Aug 13 '14 at 01:44
  • Is there another way to get the view size? I have to initialize animator with the view width. And the animator may be started before the onGlobalLayout was called. – Kimi Chiu May 10 '16 at 05:05
  • removing listener inside `onGlobalLayout()` gives `IndexOutOfBoundError` sometimes. – Jimit Patel Oct 24 '16 at 07:08
  • 2
    in API 16+ you should use `removeOnGlobalLayoutListener()` instead of `removeGlobalOnLayoutListener()`. [see this](http://stackoverflow.com/a/15578844/1074799) – S.M.Mousavi Dec 12 '16 at 17:45
19

(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.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
3

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)
}
pavelperc
  • 119
  • 5
2

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!
 }
Community
  • 1
  • 1
spechter
  • 2,058
  • 1
  • 17
  • 23
  • More kludgy than right. That'll trigger a bunch like when you get a dialog or basically anything that changes the window focus. Seems better to use a predraw listener. – Tatarize Dec 25 '15 at 03:05
  • Yep, fair enough. I probably should have said "short" rather then "elegant". Answer by kcoppock is more elegant, but it's a bit more guff for a simple problem. Efficient and correct it surely is though. – spechter Jan 04 '16 at 10:35
  • Seems like predraw rather than layout should be better, it's pretty expressly correct. "Callback method to be invoked when the view tree is about to be drawn. At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs." – Tatarize Jan 04 '16 at 11:00
  • Works great.. Thanks :) – Fathima km Sep 04 '17 at 14:42
1

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);
                }
            });
}
jd45p8
  • 11
  • 3
-2

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.

Maxim Efimov
  • 2,747
  • 1
  • 19
  • 25