You can't determine the height of the list in pixels right after the application start because the application doesn't know what that height should be then.
The app goes in sequence:
- Acitivity Launched
- onCreate()
- onStart()
- onResume()
- Activity Running
I'm not sure how you interpret that any of those guarantee you an adequate measurement of a layout element.
The common Android UI doesn't just plop art out at specific locations and "poof" its always in the perfect place, because the Android UI is designed to be adaptive to whatever device it is running on. It has to layout the UI, balancing out constraints and alignments, even having to choose the appropriate drawables and layouts based on the size and pixel density of the display. Then it has to populate your list view.
You will not be able to get the height of any widget until the OS has calculated just what that height should be. That is not part of the view lifecycle.
Here is one place that should work (showing modified code from this famous solution:)
final ListView tv = (ListView)findViewById(R.id.your_widget);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
LayerDrawable ld = (LayerDrawable)tv.getBackground();
ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0);
ViewTreeObserver obs = tv.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});