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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.