title: Why is 0dp considered a performance enhancement?
body: <p>An <strong>answer</strong> at the end of this question has been filled out, combining remarks and solutions.</p>
  
  <h2>Question</h2>
  
  <p>I searched around but haven't found anything that really explains why <em>Android Lint</em> as well as some <em>Eclipse</em> hints suggest replacing some <code>layout_height</code> and <code>layout_width</code> values with <code>0dp</code>.</p>
  
  <p>For example, I have a <code>ListView</code> that was suggested to be changed</p>
  
  <p><strong>Before</strong></p>
  
  <pre><code>&lt;ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p><strong>After</strong></p>
  
  <pre><code>&lt;ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>Similarly, it suggested changes to a <em>ListView item</em>. These all look the same before and after the changes, but I'm interested in understanding why these are performance boosters.</p>
  
  <p>Anyone have an explanation of why? If it helps, here is general layout with the <code>ListView</code>.</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
      &lt;ImageView
          android:id="@+id/logo_splash"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
      &lt;/ImageView&gt;
  
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:background="@color/background"
          android:layout_below="@id/logo_splash"&gt;
  
          &lt;ListView
              android:id="@android:id/list"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"&gt;
          &lt;/ListView&gt;
  
          &lt;TextView
              android:id="@android:id/empty"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/no_upcoming" /&gt;
  
      &lt;/LinearLayout&gt;        
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <h2>Answer</h2>
  
  <p>I'm putting in an answer here because it's really a combination of answers and referenced links below. If I'm wrong on something, do let me know.</p>
  
  <p>From <a href="https://stackoverflow.com/questions/7220404/what-is-the-trick-with-0dip-layout-height-or-layouth-width">What is the trick with 0dip layout_height or layouth_width?</a></p>
  
  <p>There are 3 general layout attributes that work with <em>width</em> and <em>height</em></p>
  
  <ol>
  <li><code>android:layout_height</code></li>
  <li><code>android:layout_width</code></li>
  <li><code>android:layout_weight</code></li>
  </ol>
  
  <p>When a <code>LinearLayout</code> is <em>vertical</em>, then the <code>layout_weight</code> will effect the <em>height</em> of the child <code>View</code>s (<code>ListView</code>). Setting the <code>layout_height</code> to <code>0dp</code> will cause this attribute to be ignored.</p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>&lt;LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"&gt;
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"&gt;
      &lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>When a <code>LinearLayout</code> is <em>horizontal</em>, then the <code>layout_weight</code> will effect the <em>width</em> of the child <code>View</code>s (<code>ListView</code>). Setting the <code>layout_width</code> to <code>0dp</code> will cause this attribute to be ignored.</p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>&lt;LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal"&gt;
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"&gt;
      &lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The reason to want to ignore the attribute is that if you didn't ignore it, it would be used to calculate the layout which uses more CPU time. </p>
  
  <p>Additionally this prevents any confusion over what the layout should look like when using a combination of the three attributes. This is highlighted by <em>@android developer</em> in an answer below.</p>
  
  <p>Also, <em>Android Lint</em> and <em>Eclipse</em> both say to use <code>0dip</code>. From that answer below, you can use <code>0dip</code>, <code>0dp</code>, <code>0px</code>, etc since a zero size is the same in any of the units.</p>
  
  <p><strong>Avoid wrap_content on ListView</strong></p>
  
  <p>From <a href="https://stackoverflow.com/questions/4270278/layout-width-of-a-listview/4270471#4270471">Layout_width of a ListView</a></p>
  
  <p>If you've ever wondered why <code>getView(...)</code> is called so many times like I have, it turns out to be related to <code>wrap_content</code>.</p>
  
  <p>Using <code>wrap_content</code> like I was using above will cause all child <code>View</code>s to be measured which will cause further CPU time. This measurement will cause your <code>getView(...)</code> to be called. I've now tested this and the number of times <code>getView(...)</code> is called is reduced dramatically.</p>
  
  <p>When I was using <code>wrap_content</code> on two <code>ListView</code>s, <code>getView(...)</code> was called 3 times for each row on one <code>ListView</code> and 4 times for each row on the other.</p>
  
  <p>Changing this to the recommended <code>0dp</code>, <code>getView(...)</code> was called only once for each row. This is quite an improvement, but has more to do with avoiding <code>wrap_content</code> on a <code>ListView</code> than it does the <code>0dp</code>.</p>
  
  <p>However the suggestion of <code>0dp</code> does substantially improve performance because of this.</p>