New Compose Multiplatform components arrived on Composables UICheck it out →

Modifier in Compose Ui

focusRequester

Common

Add this modifier to a component to request changes to focus.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.ui:ui:1.7.0-beta04")
}

Overloads


fun Modifier.focusRequester(focusRequester: FocusRequester)

Code Example

RequestFocusSample

@Composable
@Sampled
fun RequestFocusSample() {
    val focusRequester = remember { FocusRequester() }
    var color by remember { mutableStateOf(Black) }
    Box(
        Modifier
            .clickable { focusRequester.requestFocus() }
            .border(2.dp, color)
            // The focusRequester should be added BEFORE the focusable.
            .focusRequester(focusRequester)
            // The onFocusChanged should be added BEFORE the focusable that is being observed.
            .onFocusChanged { color = if (it.isFocused) Green else Black }
            .focusable()
    )
}