FocusState

Interface

Common
interface FocusState

The focus state of a FocusTargetNode. Use onFocusChanged or onFocusEvent modifiers to access FocusState.

Properties

Common
val isFocused: Boolean

Whether the component is focused or not.

Returns

true if the component is focused, false otherwise.
Common
val hasFocus: Boolean

Whether the focus modifier associated with this FocusState has a child that is focused.

Returns

true if a child is focused, false otherwise.
Common
val isCaptured: Boolean

Whether focus is captured or not. A focusable component is in a captured state when it wants to hold onto focus. (Eg. when a text field has an invalid phone number). When we are in a captured state, clicking on other focusable items does not clear focus from the currently focused item.

You can capture focus by calling focusRequester.captureFocus() and free focus by calling focusRequester.freeFocus().

Returns

true if focus is captured, false otherwise.

Code Examples

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()
    )
}