How to get a layout size before it's displayed? <p>I inflate a layout containing an <code>ImageView</code> and a <code>RelativeLayout</code>. The layout has its width set to <code>match_parent</code> and its height set to <code>wrap_content</code>. The height is determined by the <code>ImageView</code>, but the image set to the <code>ImageView</code> is loaded dynamically from the internet.</p> <p>Since I know the image ratio, I'd like to set the size of the <code>ImageView</code> before it's displayed to avoid a jump in the UI due to the change in the layout height when the image is set.</p> <p>To set the size I need the layout width, to compute the <code>ImageView</code> height.</p> <p>I tried</p> <pre><code>int width = header.getMeasuredWidth(); </code></pre> <p>but since the layout is not drawn yet it returns 0</p> <p>I also tried to use <code>measure</code> before, as suggested <a href="http://stackoverflow.com/a/5383811/326849">here</a></p> <pre><code>header.measure(0, 0); int width = header.getMeasuredWidth(); </code></pre> <p>but <code>measure</code> returns a <code>NullPointerException</code></p> <p>How can I do that?</p> <p><b>list_header.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;ImageView android:id="@+id/pic" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;RelativeLayout android:id="@+id/header_text_container" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="#789987"&gt; &lt;/RelativeLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p><b>MyListFragment.java</b></p> <pre><code>@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mListView = (ListView) getView().findViewById(R.id.listview); View header = getActivity().getLayoutInflater().inflate(R.layout.list_header, null); int width = header.getMeasuredWidth(); // 0 because not drawn yet ImageView pic = (ImageView) header.findViewById(R.id.pic); pic.setLayoutParams(new LayoutParams(width, (int) (width/imgRatio))); pic.invalidate(); header.invalidate(); /* ... */ } </code></pre>