how can i specify which items gets focused first i...
# compose
a
how can i specify which items gets focused first in a layout? ie. i want right to be focused first
Copy code
val (right, left) = remember { FocusRequester.createRefs() }
        Row {
            Button(onClick = {}, modifier = Modifier.focusRequester(left)) {
                Text("Left")
            }
            Button(onClick = {}, modifier = Modifier.focusRequester(right)) {
                Text("Right")
            }
        }
This seems to work:
Copy code
val (right, left) = remember { FocusRequester.createRefs() }

        Row(
            Modifier
                .onFocusChanged { if (it.hasFocus) right.requestFocus() }
                .focusGroup()
        ) {
            Button(onClick = {}, modifier = Modifier.focusRequester(left).focusProperties {
                previous = right
                next = right
            }) {
                Text("Left")
            }
            Button(onClick = {}, modifier = Modifier.focusRequester(right).focusProperties {
                previous = left
                next = left
            }) {
                Text("Right")
            }
        }
t
I think the normal way is to use focusProperties on the Row with enter = right
And if you want to support dpad you need to also handle right / left in the items properties
a
nice one. replaced with:
Copy code
.focusProperties { onEnter = { right.requestFocus() } }
because enter is deprecated
t
Beware that focus on Compose is highly special with how it choose where to go 😞
❤️ 1
a
what im working on is either going to turn out great or terrible. no in betweens
😂 1
t
Good luck 🙂 I'm still unable to properly do DPAD navigation on Android TV with compose due to the number of issues like https://issuetracker.google.com/issues/367440163 (See my video at the end)
At this point you nearly need to manage every single interactions by hand, and good luck with focusRestorer outside of lazystuff.
🙏 1