Build apps faster with our new App builder! Check it out →

focusRequester

Common

Modifier in Compose Ui

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

Last updated:

Installation

dependencies {
   implementation("androidx.compose.ui:ui:1.8.0-alpha04")
}

Overloads


fun Modifier.focusRequester(focusRequester: FocusRequester): Modifier

Code Example

RequestFocusSample

@Composable
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()
    )
}
by @alexstyl