Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I call getMeasuredWidth() or getWidth() for layout in onResume they return 0

If I call getMeasuredWidth() or getWidth() for layout in onResume they returns 0. I think that view it's not drawn yet in this moment.

Also I think that I need to put getMeasuredWidth() or getWidth() in callback method called after layout is drawn and view measurements are known. What android callback method should I use?

like image 807
mobiledev Alex Avatar asked Aug 04 '11 09:08

mobiledev Alex


1 Answers

You cannot use the width/height/getMeasuredWidth/getMeasuredHeight on a View before the system renders it (typically from onCreate/onResume).

Simple solution for this is to post a Runnable to the layout. The runnable will be executed after the View has been laid out.

BoxesLayout = (RelativeLayout) findViewById(R.id.BoxesLinearLayout); BoxesLayout.post(new Runnable() {     @Override     public void run() {         int w = BoxesLayout.getMeasuredWidth();         int h = BoxesLayout.getMeasuredHeight();          ...     } }); 
like image 199
Adorjan Princz Avatar answered Sep 29 '22 12:09

Adorjan Princz