hi there, question about android-tv, could sb help...
# compose-android
m
hi there, question about android-tv, could sb help me with focusProperties? I have Column with focusable Boxes with FocusRequesters, the point is to indicate which one should be focused on Column focus enter using d-pad. With Modifier.focusProperties { enter = { selectedFocusRequester }} it dosnt work. Am I miss sth or I missunderstund this modifier? As far as I checked this
enter = {} or exit ={}
scopes are not triggered at all, only
next, previous, up, down...
work fine.
v
In which composable do you have the
focusProperties
modifier?
m
@vide I tried with Column that contains focusable Boxes and with the Column's parent node which is Box. No difference
v
It behaves a little unintuitively, you need to make the parent a focus node. Try adding a
focusGroup
after the
focusProperties
like this:
Copy code
Box(Modifier.focusProperties { ... }.focusGroup()) { .. }
Otherwise the focusProperties apply to the closest child focus nodes, which you probably don't want
m
@vide so if I have simplified case like this:
Copy code
Box(modifier = Modifier) {
    Column(modifier = Modifier) {
        Box(modifier = Modifier.focusable())
        Box(modifier = Modifier.focusable())
        Box(modifier = Modifier.focusable())
    }
}
the root Box should have
focusProperfies{}.focusGroup()
?
@vide I followed your hint and put modifier like this
Modifier.focusProperties { ... }.focusGroup()
in multiple combinations, I have achieved that enter = {} is triggered and correct node is focused but only for the first focus attempt, Im not able to update this property
Do you have any ideas?
v
I'm not sure what kind of visual appearance you are looking for the UI, you probably want the Column to be focusable as well? (It also implicitly contains a
focusTarget
. You only need it in the case you want to use focusProperties but don't want to make it focusable)
So something like
Copy code
Column(Modifier.focusProperties { enter = focusRequester1 }.focusable()) {
    Box(Modifier.focusable())
    Box(Modifier.focusRequester(focusRequester1).focusable())
    Box(Modifier.focusable())
}
m
@vide so Im looking for solution to point which column element should be focused when using d-pad I switch focus to Column, Column itself should not get focus but its child nodes should. I think that this sample will explain this case:
Copy code
@Composable
fun Sample() {
    val requester1 = remember { FocusRequester() }
    val requester2 = remember { FocusRequester() }
    val requester3 = remember { FocusRequester() }
    var selected by remember { mutableStateOf(requester3) }
    Box(
        modifier = Modifier
            .focusProperties { enter = { selected } }
            .focusGroup(),
    ) {
        Column(modifier = Modifier) {
            Box(modifier = Modifier.focusRequester(requester1).focusable().clickable { selected = requester1 })
            Box(modifier = Modifier.focusRequester(requester2).focusable().clickable { selected = requester2 })
            Box(modifier = Modifier.focusRequester(requester3).focusable().clickable { selected = requester3 })
        }
    }
}
This works on first attempt,
enter ={}
scope is triggered, requester works fine, correct Box is focused, but when I change focus to any node outside of this and than return to this Column, focus search do not trigger
enter ={}
and Default behaviour is is triggered