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 }, )
Use the Player composable
If you use the Player composable from the media3-ui-compose-material3
library, D-pad navigation is supported by default. The Player and
PlayerDefaults components have an automatic FocusRequester instance that
traverses the components with the D-pad, so you don't need additional code or
Modifier.focusRequester to handle focus and key events:
The PlayerDefaults are arranged in top, center, and bottom slots so the
D-pad navigation feels intuitive. For any customizations and overrides to those
slots, ensure you provide a well-navigable component. Note that most Compose
components, such as Slider or Row containing buttons, manage focus
out-of-the-box.