D-pad navigation on Android TV
The remote control of Android TV has a D-pad control that sends commands that
arrive as key events at dispatchKeyEvent(KeyEvent)
of your Activity
. These
need to be delegated to the PlayerView
:
Kotlin
override fun dispatchKeyEvent(event: KeyEvent?): Boolean{ return playerView.dispatchKeyEvent(event!!) || super.dispatchKeyEvent(event) }
Java
@Override public boolean dispatchKeyEvent(KeyEvent event) { return playerView.dispatchKeyEvent(event) || super.dispatchKeyEvent(event); }
Requesting focus for the PlayerView
is important for navigating playback
controls and skipping ads. Consider requesting the focus in onCreate
of the
Activity
:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... playerView.requestFocus() // ... }
Java
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ... playerView.requestFocus(); // ... }
If you are using Compose on Android TV, you need to make the AndroidView
focusable and delegate the event by passing the modifier parameter into the
AndroidView
accordingly:
AndroidView(
modifier = modifier
.focusable()
.onKeyEvent { playerView.dispatchKeyEvent(it.nativeKeyEvent) },
factory = { playerView }
)