We just launched Compose Examples featuring over 150+ components! Check it out →

onFocusChanged

Common

Modifier in Compose Ui

Add this modifier to a component to observe focus state events. [onFocusChanged] is invoked when the focus state changes. The [onFocusChanged] modifier listens to the state of the first [focusTarget] following this modifier.

Last updated:

Installation

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

Overloads


fun Modifier.onFocusChanged(onFocusChanged: (FocusState) -> Unit): Modifier

Code Example

FocusableSample

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