# AsyncLayoutInflater

Artifact: [androidx.asynclayoutinflater:asynclayoutinflater](https://developer.android.com/jetpack/androidx/releases/asynclayoutinflater) [View Source](https://cs.android.com/search?q=file:androidx/asynclayoutinflater/view/AsyncLayoutInflater.java+class:androidx.asynclayoutinflater.view.AsyncLayoutInflater) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/asynclayoutinflater#1.0.0)

*** ** * ** ***


```
public final class AsyncLayoutInflater
```

<br />

*** ** * ** ***

Helper class for inflating layouts asynchronously. To use, construct an instance of `https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater` on the UI thread and call `https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater#inflate(int,android.view.ViewGroup,androidx.asynclayoutinflater.view.AsyncLayoutInflater.OnInflateFinishedListener)`. The `https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener` will be invoked on the UI thread is no executor is passed, otherwise, it is called on the given executor.

This is intended for parts of the UI that are created lazily or in response to user interactions. This allows the UI thread to continue to be responsive \&animate while the relatively heavy inflate is being performed.

For a layout to be inflated asynchronously it needs to have a parent whose `https://developer.android.com/reference/android/view/ViewGroup.html#generateLayoutParams(android.util.AttributeSet)` is thread-safe and all the Views being constructed as part of inflation must not create any `https://developer.android.com/reference/android/os/Handler.html`s or otherwise call `https://developer.android.com/reference/android/os/Looper.html#myLooper()`. If the layout that is trying to be inflated cannot be constructed asynchronously for whatever reason, `https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater` will automatically fall back to inflating on the UI thread.

NOTE that the inflated View hierarchy is NOT added to the parent. It is equivalent to calling `https://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup, boolean)` with attachToRoot set to false. Callers will likely want to call `https://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View)` in the `https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener` callback at a minimum.

This inflater does not support setting a `https://developer.android.com/reference/android/view/LayoutInflater.Factory.html` nor `https://developer.android.com/reference/android/view/LayoutInflater.Factory2.html`. Similarly it does not support inflating layouts that contain fragments.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater#AsyncLayoutInflater(android.content.Context)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` |
| `https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater#AsyncLayoutInflater(android.content.Context,androidx.asynclayoutinflater.view.AsyncLayoutFactory)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutFactory asyncLayoutFactory )` |

| ### Public methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/UiThread https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater#inflate(int,android.view.ViewGroup,androidx.asynclayoutinflater.view.AsyncLayoutInflater.OnInflateFinishedListener)( @https://developer.android.com/reference/androidx/annotation/LayoutRes int resid, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/view/ViewGroup.html parent, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener callback )` Triggers view inflation on background thread. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/UiThread https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater#inflate(int,android.view.ViewGroup,java.util.concurrent.Executor,androidx.asynclayoutinflater.view.AsyncLayoutInflater.OnInflateFinishedListener)( @https://developer.android.com/reference/androidx/annotation/LayoutRes int resid, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/view/ViewGroup.html parent, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener callback )` Triggers inflation on a background thread. |

## Public constructors

### AsyncLayoutInflater

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/asynclayoutinflater#1.0.0)

```
public AsyncLayoutInflater(@NonNull Context context)
```

### AsyncLayoutInflater

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/asynclayoutinflater#1.1.0)

```
public AsyncLayoutInflater(
    @NonNull Context context,
    @NonNull AsyncLayoutFactory asyncLayoutFactory
)
```

## Public methods

### inflate

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/asynclayoutinflater#1.0.0)

```
@UiThread
public void inflate(
    @LayoutRes int resid,
    @Nullable ViewGroup parent,
    @NonNull AsyncLayoutInflater.OnInflateFinishedListener callback
)
```

Triggers view inflation on background thread.

### inflate

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/asynclayoutinflater#1.1.0)

```
@UiThread
public void inflate(
    @LayoutRes int resid,
    @Nullable ViewGroup parent,
    @Nullable Executor callbackExecutor,
    @NonNull AsyncLayoutInflater.OnInflateFinishedListener callback
)
```

Triggers inflation on a background thread. It triggers the `https://developer.android.com/reference/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener` on the given executor instead of the main thread.