Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Last active August 29, 2021 02:39
Show Gist options
  • Save vxhviet/345dc70960d85047ab04 to your computer and use it in GitHub Desktop.
Save vxhviet/345dc70960d85047ab04 to your computer and use it in GitHub Desktop.
Why is 0dp considered a performance enhancement? & Avoid wrap_content on ListView

Source: StackOverflow

Question: [What is the trick with 0dip layout_height or layouth_width?] (http://stackoverflow.com/questions/7220404/what-is-the-trick-with-0dip-layout-height-or-layouth-width)

Answer: There are 3 general layout attributes that work with width and height

  1. android:layout_height
  2. android:layout_width
  3. android:layout_weight

When a LinearLayout is vertical, then the layout_weight will effect the height of the child Views (ListView). Setting the layout_height to 0dp will cause this attribute to be ignored.

Example

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ListView>
</LinearLayout>

When a LinearLayout is horizontal, then the layout\_weight will effect the width of the child Views (ListView). Setting the layout\_width to 0dp will cause this attribute to be ignored.

Example

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <ListView
        android:id="@android:id/list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </ListView>
</LinearLayout>

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.

Additionally this prevents any confusion over what the layout should look like when using a combination of the three attributes. This is highlighted by @android developer in an answer below.

Also, Android Lint and Eclipse both say to use 0dip. From that answer below, you can use 0dip, 0dp, 0px, etc since a zero size is the same in any of the units.

Question: Avoid wrap_content on ListView

Answer: From Layout_width of a ListView

If you've ever wondered why getView(...) is called so many times like I have, it turns out to be related to wrap_content.

Using wrap_content like I was using above will cause all child Views to be measured which will cause further CPU time. This measurement will cause your getView(...) to be called. I've now tested this and the number of times getView(...) is called is reduced dramatically.

When I was using wrap_content on two ListViews, getView(...) was called 3 times for each row on one ListView and 4 times for each row on the other.

Changing this to the recommended 0dp, getView(...) was called only once for each row. This is quite an improvement, but has more to do with avoiding wrap_content on a ListView than it does the 0dp.

However the suggestion of 0dp does substantially improve performance because of this.

@piusmwilson
Copy link

Thank you. This is very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment