What are some ways to improve & optimize View Layout performance?

Copied from Android Training

  1. Avoid deep level layouts

    which are bad for performance. Consider using flatter layouts such as ConstraintLayout to reduce initialization and drawing time. This is particularly important for ListView or GridView which layout is inflated repeatedly.

  2. Merge root frame

    If a FrameLayout is the root of a layout and does not provide background or padding etc, it can be replaced with a merge tag which is slightly more efficient.

  3. Re-using Layouts with <include/>

    Reusing layouts is particularly powerful as it allows you create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text. It also means that any elements of your application that are common across multiple layouts can be extracted, managed separately, then included in each layout.

  4. Use ViewStub to Delay Loading of Views

    Deferring loading resources is an important technique to use when you have complex views that your app might need in the future. You can implement this technique by defining a ViewStub for those complex and rarely used views.

  5. Making ListView Scrolling Smooth

    • The key to a smoothly scrolling ListView is to keep the application’s main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread.

    • Hold View Objects in a ViewHolder.

      Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.