FocusManager


Summary

Public functions

Unit

Call this function to clear focus from the currently focused component, and set the focus to the root focus modifier.

Cmn
Boolean
moveFocus(focusDirection: FocusDirection)

Moves focus in the specified direction.

Cmn

Public functions

clearFocus

fun clearFocus(force: Boolean = false): Unit

Call this function to clear focus from the currently focused component, and set the focus to the root focus modifier.

import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.dp

val focusManager = LocalFocusManager.current
Column(Modifier.clickable { focusManager.clearFocus() }) {
    Box(Modifier.focusable().size(100.dp))
    Box(Modifier.focusable().size(100.dp))
    Box(Modifier.focusable().size(100.dp))
}
Parameters
force: Boolean = false

: Whether we should forcefully clear focus regardless of whether we have any components that have Captured focus.

moveFocus

fun moveFocus(focusDirection: FocusDirection): Boolean

Moves focus in the specified direction.

If you are not satisfied with the default focus order, consider setting a custom order using Modifier.focusProperties().

import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.platform.LocalFocusManager

val focusManager = LocalFocusManager.current
Column {
    Row {
        Box(Modifier.focusable())
        Box(Modifier.focusable())
    }
    Row {
        Box(Modifier.focusable())
        Box(Modifier.focusable())
    }
    Button(onClick = { focusManager.moveFocus(FocusDirection.Right) }) { Text("Right") }
    Button(onClick = { focusManager.moveFocus(FocusDirection.Left) }) { Text("Left") }
    Button(onClick = { focusManager.moveFocus(FocusDirection.Up) }) { Text("Up") }
    Button(onClick = { focusManager.moveFocus(FocusDirection.Down) }) { Text("Down") }
}
Returns
Boolean

true if focus was moved successfully. false if the focused item is unchanged.