When Activity onResume() is called, it indicates that the app is in the foreground, is visible, and is in a state of interacting with the user. However, when the method is executed for the first time, it cannot obtain the true width of the View. Why?
Asked
Active
Viewed 83 times
0
-
I just want to know why, since it is visible in onResume, does that mean that View has already been drawn? Already drawn, why is width not available correctly? Or is the description about onResume inaccurate? – Zero Sep 18 '19 at 02:40
-
The views have not yet been measured and drawn when `onResume()` is called the first time. This happens later. Please see the linked question and this answer: https://stackoverflow.com/a/24035591/769265 – David Wasser Sep 18 '19 at 17:38
1 Answers
0
The solution to your issue is that you need to manually set the view to calculate the width when the UI has finished loading.
Here is some sample code that gets the width and height of a Textview after the UI has loaded.
this.my_textview.post(new Runnable() {
@Override
public void run() {
int x = my_textview.getWidth();
int y = my_textview.getHeight();
// TODO: Utilize the values here.
}
});
As to the why, This Post explains things quite well as it has to do with the messages queue.

PGMacDesign
- 6,092
- 8
- 41
- 78
-
I just want to know why, since it is visible in onResume, does that mean that View has already been drawn? Already drawn, why is width not available correctly? Or is the description about onResume inaccurate? – Zero Sep 18 '19 at 02:40