title: Android ListView: Can not center items on start up, due to Null Pointer Exception
body: <p>This is my first ever post here and I'm a dumb novice, so I hope someone out there can both help me and excuse my ignorance.</p>
  
  <p>I have a ListView which is populated with an ArrayAdapter. When I either scroll or click, I want the selected item, or the item nearest the vertical center, to be forced to the exact vertical center of the  screen. If I call listView.setSelection(int position) it aligns the selected position at the top of the screen, so I need to use listView.setSelectionFromTop(position, offset) instead. To find my offset, I take half of the View's height from the half of the ListView's height. </p>
  
  <p>So, I can vertically center my item easy enough, within OnItemClick or OnScrollStateChanged, with the following:</p>
  
  <pre><code>        int x = listView.getHeight();
          int y = listView.getChildAt(0).getHeight();                 
          listView.setSelectionFromTop(myPosition, x/2 - y/2);
  </code></pre>
  
  <p>All this works fine. My problem is with the initial ListView setup. I want an item to be centered when the activity starts, but I can't because I get a NullPointerException from:</p>
  
  <pre><code>int y = listView.getChildAt(0).getHeight();
  </code></pre>
  
  <p>I understand this is because the ListView has not yet rendered, so it has no children, when I call this from OnCreate() or OnResume().</p>
  
  <p>So my question is simply: how can I force my ListView to render at startup so I can get the height value I need? Or, alternatively, is there any other way to center items vertically within a ListView?</p>
  
  <p>Thanks in advance for any help!</p>