# 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)

*** ** * ** ***


```
class AsyncLayoutInflater
```

<br />

*** ** * ** ***

Helper class for inflating layouts asynchronously. To use, construct an instance of `https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater` on the UI thread and call `https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater#inflate(int,android.view.ViewGroup,androidx.asynclayoutinflater.view.AsyncLayoutInflater.OnInflateFinishedListener)`. The `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/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 |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater#AsyncLayoutInflater(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` |
| `https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater#AsyncLayoutInflater(android.content.Context,androidx.asynclayoutinflater.view.AsyncLayoutFactory)( context: https://developer.android.com/reference/android/content/Context.html, asyncLayoutFactory: https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutFactory )` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/UiThread https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater#inflate(int,android.view.ViewGroup,androidx.asynclayoutinflater.view.AsyncLayoutInflater.OnInflateFinishedListener)( resid: @https://developer.android.com/reference/kotlin/androidx/annotation/LayoutRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, parent: https://developer.android.com/reference/android/view/ViewGroup.html?, callback: https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener )` Triggers view inflation on background thread. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/UiThread https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater#inflate(int,android.view.ViewGroup,java.util.concurrent.Executor,androidx.asynclayoutinflater.view.AsyncLayoutInflater.OnInflateFinishedListener)( resid: @https://developer.android.com/reference/kotlin/androidx/annotation/LayoutRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, parent: https://developer.android.com/reference/android/view/ViewGroup.html?, callbackExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html?, callback: https://developer.android.com/reference/kotlin/androidx/asynclayoutinflater/view/AsyncLayoutInflater.OnInflateFinishedListener )` 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)

```
AsyncLayoutInflater(context: Context)
```

### AsyncLayoutInflater

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

```
AsyncLayoutInflater(
    context: Context,
    asyncLayoutFactory: AsyncLayoutFactory
)
```

## Public functions

### inflate

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

```
@UiThread
fun inflate(
    resid: @LayoutRes Int,
    parent: ViewGroup?,
    callback: AsyncLayoutInflater.OnInflateFinishedListener
): Unit
```

Triggers view inflation on background thread.

### inflate

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

```
@UiThread
fun inflate(
    resid: @LayoutRes Int,
    parent: ViewGroup?,
    callbackExecutor: Executor?,
    callback: AsyncLayoutInflater.OnInflateFinishedListener
): Unit
```

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