androidx.compose.ui.viewinterop
Classes
AndroidViewHolder |
A base class used to host a View inside Compose. |
Annotations
InternalInteropApi |
Top-level functions summary
Unit |
AndroidView(viewBlock: (Context) -> T, modifier: Modifier = Modifier, update: (T) -> Unit = NoOpUpdate) |
Unit |
AndroidViewBinding(bindingBlock: (LayoutInflater, ViewGroup, Boolean) -> T, modifier: Modifier = Modifier, update: T.() -> Unit = {}) Composes an Android layout resource in the presence of ViewBinding. |
Unit | |
Unit | |
VM |
viewModel(key: String? = null, factory: ViewModelProvider.Factory? = null) Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity) |
VM |
viewModel(modelClass: Class<VM>, key: String? = null, factory: ViewModelProvider.Factory? = null) Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity) |
Top-level properties summary
View.() -> Unit |
An empty update block used by AndroidView. |
Top-level functions
AndroidView
@Composable fun <T : View> AndroidView(
viewBlock: (Context) -> T,
modifier: Modifier = Modifier,
update: (T) -> Unit = NoOpUpdate
): Unit
Composes an Android View obtained from viewBlock. The viewBlock block will be called exactly once to obtain the View to be composed, and it is also guaranteed to be invoked on the UI thread. Therefore, in addition to creating the viewBlock, the block can also be used to perform one-off initializations and View constant properties' setting. The update block can be run multiple times (on the UI thread as well) due to recomposition, and it is the right place to set View properties depending on state. When state changes, the block will be reexecuted to set the new properties. Note the block will also be ran once right after the viewBlock block completes.
import android.widget.TextView import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.viewinterop.AndroidView // Compose a TextView. AndroidView({ context -> TextView(context).apply { text = "This is a TextView" } }) // Compose a View and update its size based on state. Note the modifiers. var size by remember { mutableStateOf(20) } AndroidView(::View, Modifier.clickable { size += 20 }.background(Color.Blue)) { view -> view.layoutParams = ViewGroup.LayoutParams(size, size) }
Parameters | |
---|---|
viewBlock: (Context) -> T | The block creating the View to be composed. |
modifier: Modifier = Modifier | The modifier to be applied to the layout. |
update: (T) -> Unit = NoOpUpdate | The callback to be invoked after the layout is inflated. |
AndroidViewBinding
@Composable fun <T : ViewBinding> AndroidViewBinding(
bindingBlock: (LayoutInflater, ViewGroup, Boolean) -> T,
modifier: Modifier = Modifier,
update: T.() -> Unit = {}
): Unit
Composes an Android layout resource in the presence of ViewBinding. The binding is obtained from the bindingBlock block, which will be called exactly once to obtain the ViewBinding to be composed, and it is also guaranteed to be invoked on the UI thread. Therefore, in addition to creating the ViewBinding, the block can also be used to perform one-off initializations and View constant properties' setting. The update block can be run multiple times (on the UI thread as well) due to recomposition, and it is the right place to set View properties depending on state. When state changes, the block will be reexecuted to set the new properties. Note the block will also be ran once right after the bindingBlock block completes.
import androidx.compose.ui.viewinterop.AndroidViewBinding // Inflates and composes sample_layout.xml and changes the color of the `second` View. // The `second` View is part of sample_layout.xml. AndroidViewBinding(SampleLayoutBinding::inflate) { second.setBackgroundColor(Color.GRAY) }
Parameters | |
---|---|
bindingBlock: (LayoutInflater, ViewGroup, Boolean) -> T | The block creating the ViewBinding to be composed. |
modifier: Modifier = Modifier | The modifier to be applied to the layout. |
update: T.() -> Unit = {} | The callback to be invoked after the layout is inflated. |
emitView
@Composable fun <T : View>emitView(
ctor: (Context) -> T,
update: (T) -> Unit
): Unit
Deprecated.
emitView
@Composable fun <T : ViewGroup>emitView(
ctor: (Context) -> T,
update: (T) -> Unit,
children: () -> Unit
): Unit
Deprecated.
viewModel
@Composable inline fun <reified VM : ViewModel> viewModel(
key: String? = null,
factory: ViewModelProvider.Factory? = null
): VM
Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity)
The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).
Parameters | |
---|---|
key: String? = null | The key to use to identify the ViewModel. |
Return | |
---|---|
A | ViewModel that is an instance of the given VM type. |
viewModel
@Composable fun <VM : ViewModel> viewModel(
modelClass: Class<VM>,
key: String? = null,
factory: ViewModelProvider.Factory? = null
): VM