The best way to get the width and height of View in Android

The best way to get the width and height of View in Android

In Android, there are usually multiple ways to get the width and height of a View. Android's view layout and measurement are performed asynchronously. At some life cycle stages, the width and height of a control may not be determined. The following are several common methods for getting the width and height of a control:

  • 「Override onMeasure method」If it is a custom View class, you can get the width and height of the control in the onMeasure method. Note that the onMeasure method is called during the measurement phase, at which time the width and height of the control may have been determined by the layout parameter settings of the parent control or measureSpec.
  • The two methods getMeasuredWidth() and getMeasuredHeight() return the width and height values ​​determined by the control during the measurement phase. If the control has not been measured (for example, called directly in the onCreate method), the returned value may be 0.
  • "Use getWidth() and getHeight()" to return the final width and height values ​​determined by the control during the layout phase. If called before the layout phase (for example, in the onCreate method), it may also return 0.
  • 「Use ViewTreeObserver and OnGlobalLayoutListener」This is a reliable way to get the final width and height of the control. You can listen to the global layout changes of the control by adding a ViewTreeObserver to the control and registering an OnGlobalLayoutListener. When the layout of the control is completed, the onGlobalLayout method of OnGlobalLayoutListener will be called, and you can safely get the width and height of the control.

Sample code:

 View view = findViewById(R.id.view); view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); int width = view.getWidth(); int height = view.getHeight(); } });
  1. 「Use post method」The post method queues a Runnable object to the execution queue of the main thread and executes it after the drawing phase of the control. It is usually used to perform certain operations after the layout and drawing of the control are completed. The post method can be used to delay the acquisition of the width and height of the control.

Sample code:

 View view = findViewById(R.id.view); view.post(new Runnable() { @Override public void run() { int width = view.getWidth(); int height = view.getHeight(); } });
  • 「Using View.LayoutParams」In some cases, you can predict the possible width and height of a control by checking its LayoutParams. This is usually only applicable to controls that use fixed sizes (such as WRAP_CONTENT or MATCH_PARENT) or specific sizes (such as dp or px values). This method does not guarantee that the final width and height of the control are obtained.
  • The "onWindowFocusChanged" method will be called multiple times, after the View is initialized, and once when the Activity's window gains and loses focus (for example, when the Activity is onResume).

Sample code:

 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { int width = view.getMeasuredWidth(); int height = view.getMeasuredHeight(); } }

When trying to get the width and height of a control, you should consider the control's life cycle and layout process to ensure that you get the correct value at the right time.

<<:  Apple is also keeping up with the times on the front end! Did you know?

>>:  iOS 18 hidden feature, supports T9 dialing!

Recommend

TLV Partner: Israel IoT Startup Map 2015

The Internet of Things, as well as the related bi...

WeChat Android version 8.0.14 beta version released, with developer updates

[[424212]] Yesterday, Tencent WeChat team release...

How did Xiaohongshu become popular? Share 5 tips!

Online shopping is a topic that many people often...

How to use information flow advertising to increase APP downloads?

How to use information flow advertising to increa...

Android 12 new version details: follow iOS 14 to improve user privacy experience

Some developers have found that in the new versio...

Jump? Still jumping? If you jump again, you will... go to hell!

Speaking of dung beetles, everyone may be familia...

New ways of playing for top private domain brands

Nowadays, many companies are using private domain...

Why are asthma attacks so deadly? First aid experts explain

Recently, a nurse and an elderly man in Shanghai ...