Optimizing layouts

To achieve maximal UI performance, follow these points:

  • Optimize your layout hierarchies: Avoid nested layouts since it can be a performance killer! For example, you can have multiple LinearLayout views nested. Instead of this, switch to RelativeLayout. This can save your performance significantly! Nested layouts require more processing power to be used on calculations and drawing.
  • Reuse layouts if possible: Android provides <include /> to allow this.

Take a look at the following example:

    to_be_included.xml: 
    <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/main_bg" ...

Get Mastering Android Development with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.