What is the difference between getMeasuredHeight and getHeight

25,809

When you use a View, it imposes certain size limitations on its children elements. These limitations may prevent a child element from taking up the amount of space it wants to.

The documentation for the View class describes these properties as follows:

The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.

The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().

The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().

See also: http://developer.android.com/guide/topics/ui/how-android-draws.html

Share:
25,809
Lumii
Author by

Lumii

WeNote - Notes, To-do lists, Reminders & Calendar JStock Android JStock - Free Stock Market Software WeFocus - Focus, Pomodoro, Do one thing at a time

Updated on July 09, 2022

Comments

  • Lumii
    Lumii almost 2 years

    I try to call View.getMeasuredHeight and View.getHeight in onGlobalLayout. Both give me the same result.

    I was wondering, under what situation their value will be different? Is there any example to demonstrate this?

    I tried searching in google. The 1st result doesn't seem relevant to this question : Android: How to get a custom View's height and width?

  • Denny Kurniawan
    Denny Kurniawan over 5 years
    So getMeasuredWidth() and getMeasuredHeight() return value in pixels otherwise getWidth() and getHeight() return value in dp?
  • Dan Bechard
    Dan Bechard over 4 years
    @DennyKurniawan The values for all four measurements are returned in pixels.