getHeight returns 0 for all Android UI objects
Solution 1:
It's 0 because in both onCreate and onStart, the view hasn't actually been drawn yet. You can get around this by listening for when the view is actually drawn:
final TextView tv = (TextView)findViewById(R.id.venueLabel);
final ViewTreeObserver observer= tv.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
tv.getHeight()
observer.removeGlobalOnLayoutListener(this);
}
});
The call to remove the listener is there to prevent repeated invocations of your custom handler on layout changes... if you want to get those, you can omit it.
Solution 2:
In short, the views are not built yet in onCreate(), onStart(), or onResume(). Since they technically don't exist (as far as the ViewGroup is concerned), their dimensions are 0.
In long, you can go here for a better explanation on how to handle it.
How to retrieve the dimensions of a view?
Solution 3:
Use this function to get Height or Width of View
private int getHeightOfView(View contentview) {
contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//contentview.getMeasuredWidth();
return contentview.getMeasuredHeight();
}