What is the best way to make an Android TV menu?

4.2k views Asked by At

I'm currently developing an Android TV app. I find it challenging because you can't just draw a list and have the user navigate with touch. You have to programmatically scroll the list when the user hits the Dpad buttons.

I would like to create a list that is similar to the menus in the Settings screen on Android TV. Please see the video example.

So far, the solution I'm using is to create horizontal RecyclerView that updates when the user hits dpad right or left.

So in my adapter I have a method:

 public void incrementPosition() {
      if(currentSelection < getItemCount()) {
          previousSelection = currentSelection;
          notifyItemChanged(previousSelection);
          currentSelection++;
          notifyItemChanged(currentSelection);
      }
 }

Then when onBindViewHolder is called I have something like:

if(position == currentSelection) {
         //draw item to look like it's selected
     }
     else if(position == previousSelection) {
        // draw item to look it's NOT selected
     }

This will make the item appear to be selected, however the list doesn't scroll along with the new selection.

recentlyAddedRecyclerView.smoothScrollToPosition(currentSelection + 2)

(yes, I know currentSelection + 2 can go beyond the list, I have some if statements there)

The result is similar to the video posted above. However, it's not as "snappy" as I would like it to be. I just want to make sure this is the best way to implement it.

And this is a dynamic list from a network call. So I can't just make a layout with static views.

1

There are 1 answers

2
gfunk On BEST ANSWER

The v17.leanback.app libraries are the way to do it - that's what was used in your video. If you creates a fresh TV-only Android Studio Project with it's auto-generated Android Studio TV Activity it will give you a great starting point with tangible examples of header rows, customization, etc.

Just in case:

  1. Android Studio > File > New Project > name it & click Next
  2. Only select the TV Platform then click Next
  3. Select "Android TV Activity" then click Next

You'll care about the MainFragment & CardPresenter. Think of the BrowseFragment as a listview you populate with rows. Rows are filled with Cards which are built-upon FrameLayouts. RecyclerView is already integrated into leanback... I've found I have to spend a lot of time customizing them as you've noted, and just trying to find/navigate relevant online documentation. Good luck.