Skip to content

Instantly share code, notes, and snippets.

@mataanin
Last active January 8, 2018 01:23
Show Gist options
  • Save mataanin/d59f8f75c36149aece6f4777bbba40da to your computer and use it in GitHub Desktop.
Save mataanin/d59f8f75c36149aece6f4777bbba40da to your computer and use it in GitHub Desktop.
Reliable focus on a view on Android
// Sample using RxJava 1.x, Kotlin
var offSwitch = false
Observable
.interval(0, 100L, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.takeIf { !offSwitch }
.subscribe {
// try to focus every 100 milliseconds until the view or any of its children is focused
view.accessibilityFullFocus()
offSwitch = view.isAccessibilityFocused || view.hasAccessibilityFocusedChild
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment