SoFunction
Updated on 2025-04-10

Three ways to get view height from Android

This article shares with you the method of obtaining view height on Android for your reference. The specific content is as follows

getMeasuredHeight()andgetHeightThe difference
In fact, when the screen can wrap content, their values ​​are equal.
Only when the view exceeds the screen can they see their differences:
getMeasuredHeight() is the size of the actual View, and has nothing to do with the screen.
The size of getHeight is the size of the screen at this time.
When the screen is exceeded, getMeasuredHeight() is equal to getHeight() plus the size not displayed outside the screen
Specific methods
We know that in oncreate and cannot get the height and width of a view, because the layout of the View component is completed after the onResume callback.
The following are three ways
getViewTreeObserver
usegetViewTreeObserver().addOnGlobalLayoutListener()to obtain width or height.
OnGlobalLayoutListener is the internal class of ViewTreeObserver. When the layout of a view tree changes, it can be listened to by ViewTreeObserver. This is an observer (observer) who registers to monitor the view tree and is notified when the global events of the view tree change. ViewTreeObserver cannot be instantiated directly, but is obtained through getViewTreeObserver().
In addition to OnGlobalLayoutListener, ViewTreeObserver also has the following internal classes:

When the focus state in a view tree changes, the interface class of the callback function to be called

When the global layout changes in a view tree or the visual state of a view in the view tree changes, the interface class of the callback function to be called

When a view tree is about to be drawn, the interface class of the callback function to be called

When some components in a view tree scroll, the interface class of the callback function to be called

When the touch mode of a view tree changes, the interface class of the callback function to be called
Among them, we canUse OnGlobalLayoutListener to get the true height of a view

private int mHeaderViewHeight; 
private View mHeaderView; 
 
..... 
 
().addOnGlobalLayoutListener( 
 new OnGlobalLayoutListener() { 
  @Override 
  public void onGlobalLayout() { 
                                                         
   mHeaderViewHeight = (); 
   () 
     .removeGlobalOnLayoutListener(this); 
  } 
}); 

However, it should be noted that OnGlobalLayoutListener may be triggered multiple times, so after you get the height, you must log out of the OnGlobalLayoutListener.
Get in View post event
You can also get it in the post method of VIew

public class TestHeight extends Activity { 
 TextView tv; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  setContentView(.activity_activity_b); 
   tv = (TextView) findViewById(); 
  (new Runnable() { 
   @Override 
   public void run() { 
    int height= (); 
   } 
  }); 
 } 
 
 
} 

Direct measurement calculation

int intw=(0,); 
int inth=(0,); 
(intw, inth); 
int intwidth = (); 
int intheight = (); 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.