You need to measure the height and width of the view if you want to use them before rendering is complete.
The following code will help you measure the height and width of any kind.
imgView.measure( MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imgView.layout(0, 0, imgView.getMeasuredWidth(), imgView.getMeasuredHeight());
Write this code down before using the height and width of the view.
Sometimes this approach will not give the desired result. There is an alternative to this. Below code will calculate the height and width before drawing on the screen.
ViewTreeObserver viewTree = imgView.getViewTreeObserver(); viewTree.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { finalHeight = imgView.getMeasuredHeight(); finalWidth = imgView.getMeasuredWidth(); //print or do some code return true; } });
source share