getHeight returns 0 for all Android UI objects

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.

Leave a Comment