Arun Bhardwaj
07 Jan 2025
Android TV Focus Handling: Best Practices and Examples
Learn how to manage focus navigation in Android TV apps. Master focus search, D-pad navigation, and handling edge cases for a smooth TV user experience.
Android TV apps require a completely different approach to navigation compared to mobile apps. Users rely on the D-pad or remote control, making focus handling critical for usability.
Why Focus Handling Matters on Android TV
Unlike touch screens, Android TV doesn't allow direct tapping. Every UI element must be navigable using directional controls. Poor focus handling can lead to a confusing experience and make your app feel broken.
Basic Focus Handling
Android provides several attributes to control focus behavior: nextFocusUp, nextFocusDown, nextFocusLeft, nextFocusRight, and focusable. You can define these in XML or programmatically.
<Button
android:id="@+id/button1"
android:focusable="true"
android:nextFocusRight="@id/button2"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:focusable="true"
android:text="Button 2" />This ensures that pressing the right D-pad from Button 1 moves focus to Button 2.
Custom Focus Search
Sometimes default focus behavior isn’t enough. You can override onFocusSearch to customize navigation between views.
override fun onFocusSearch(focused: View, direction: Int): View? {
return if (focused.id == R.id.button3 && direction == View.FOCUS_RIGHT) {
findViewById(R.id.button5)
} else {
super.onFocusSearch(focused, direction)
}
}This approach allows non-linear navigation, which is common in media apps where grid layouts don’t always align perfectly.
Handling Edge Cases
Edge cases include focus leaving the screen, wrapping rows, or dynamically loaded lists. RecyclerView is commonly used in Android TV apps.
recyclerView.setOnChildAttachStateChangeListener(object: RecyclerView.OnChildAttachStateChangeListener {
override fun onChildViewAttachedToWindow(view: View) {
view.isFocusable = true
}
override fun onChildViewDetachedFromWindow(view: View) {}
})This ensures that newly added items are focusable when they appear.
Focus Animation and Highlighting
Adding subtle focus animations improves user experience and provides clear feedback.
view.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus) v.animate().scaleX(1.1f).scaleY(1.1f).duration = 150
else v.animate().scaleX(1f).scaleY(1f).duration = 150
}This zoom effect makes it obvious which element is currently focused.
Testing Focus Navigation
Always test your app using a real Android TV device or emulator with D-pad navigation enabled.
Best Practices
1. Ensure all interactive elements are focusable. 2. Explicitly define next focus directions for complex layouts. 3. Use RecyclerView for dynamic lists. 4. Add subtle animations for focus feedback. 5. Test across multiple TV screen sizes and devices.
Following these practices ensures your Android TV app feels smooth, intuitive, and production-ready.