Optimize android app for chromebook, make cyclic focus change in top view

  android, androidappsonchromeos

I’m trying to optimize focus change on ChromeOs in my Android app. I already read official docs, codelabs. And found that focus change order could be managed with nextFocusForwardId, nextFocusLeftId, nextFocusRightId, … I’m trying to make focus change cycling in only one view.
Take a look on this picture:

enter image description here

Normally when I press Tab key I walk through buttons menu, and then go away and walk on rest of the UI below the overlay.
When I press Right arrow I immediately go away from this menu.
I’m trying to disable this scenario, and to make focus change cyclic. So when the bottom button has focus, and I press Tab or Down, the top button should gain focus and ignore rest of ui.

I have tried this fuction:

fun makeCyclicFocusChange(viewGroup: ViewGroup) {
        for (v in viewGroup.children) {
            v.nextFocusLeftId = 0
            v.nextFocusRightId = 0
        }

        viewGroup.children.first{ it.isVisible  && it.isFocusable}.nextFocusUpId =
            viewGroup.children.last{ it.isVisible && it.isFocusable }.id
        viewGroup.children.last{ it.isVisible && it.isFocusable }.nextFocusDownId =
            viewGroup.children.first{ it.isVisible && it.isFocusable }.id
        viewGroup.children.last{ it.isVisible && it.isFocusable }.nextFocusForwardId =
            viewGroup.children.first{ it.isVisible && it.isFocusable }.id
}

But it doesn’t work. First of all, Left and Right arrows still take focus away. Also when I press Tab key on the last button, focus indeed goes to the first button but the next Tab press still take focus away from this menu.

Source: Android Questions

LEAVE A COMMENT