Interview QuestionPractical QuestionFollow-up Questions

Optimizing UI Performance With ViewStub

skydovesJaewoong Eum (skydoves)||8 min read

Optimizing UI Performance With ViewStub

ViewStub is a lightweight and invisible placeholder view used to defer the inflation of a layout until it is explicitly needed. It improves performance by avoiding the overhead of inflating views that may not be required immediately or at all during the app's lifecycle. By the end of this lesson, you will be able to:

  • Explain what ViewStub is and how it defers layout inflation.
  • Describe the lifecycle of a ViewStub from placeholder to inflated layout.
  • Identify common use cases where ViewStub reduces initial load time.
  • Recognize the limitations of ViewStub and when alternatives are more appropriate.

Key Characteristics of ViewStub

ViewStub has three defining properties that make it useful for performance optimization:

  1. Lightweight: A ViewStub has a minimal memory footprint because it does not occupy layout space or consume rendering resources until it is inflated. The system skips it entirely during measure and layout passes.

  2. Delayed Inflation: The actual layout specified in the ViewStub is only inflated when the inflate() method is called or when the ViewStub is made visible by setting its visibility to VISIBLE.

  3. One time use: Once inflated, the ViewStub is replaced by the inflated layout in the view hierarchy and cannot be reused. The ViewStub object itself is removed from the parent.

Common Use Cases

ViewStub is most useful in three scenarios:

Conditional layouts: Error messages, empty states, progress indicators, or permission request screens that may or may not be displayed depending on runtime conditions. Inflating these layouts eagerly wastes resources if they are never shown.

Reducing initial load time: Complex or resource heavy views that are not needed during the first frame can be deferred with ViewStub. This improves the time to first meaningful paint by keeping the initial view hierarchy small.

Dynamic UI elements: Content that appears in response to user actions, such as expandable sections or detail panels, can be deferred until the user requests them.

Implementation

Define a ViewStub in the XML layout with the android:layout attribute pointing to the layout resource it should inflate:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Content" />

    <ViewStub
        android:id="@+id/viewStub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/optional_content" />
</LinearLayout>

This interview continues for subscribers

Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.

Become a Sponsor