– Android TV app hands on Tutorial 12

In-app Search on Android TV application

One of the biggest difference between Android phone & Android TV is their input method. Since TV does not support touchpad, and we shouldn’t expect users to use keyboard for TV, inputting words for TV is troublesome.

Google suggests to use voice input for in Searching within TV Apps


In-app Search icon on BrowseFragment

BrowseFragment contains a design layout for search function as well, and showing in-app icon on your application is very easy. Just implement setOnSearchClickedListener, that’s all.

setSearchAffordanceColor method can be used for specifying search icon color.



In-app search icon will appear on the top-right of BrowseFragment when setOnSearchClickedListener is called.



Search function need to be implemented to answer user’s search query. To show the search query input UI and search result UI, LeanbackLibrary provides SearchFragment. This time, when request search (by either voice search button or explicitly press search button icon), we invoke this SearchFragment from SearchActivity.

Start creating SearchActivity by right click on Package name, Create → New  Activity → Blank Activity → type “SearchActivity”. After SearchActivity class and activity_search.xml layout have created, modify res/acitivity_search.xml as follows.

It attaches SearchFragment. Again, create SearchFragment by Create → New → Java class → SearchFragment, and make it a subclass of

If you build and run application here, application will crash because SearchFragment automatically start to get search query from internal speech recognizer.

Implement voice input/voice search – setSpeechRecognitionCallback

Official doc says,

If you do not supply a callback via setSpeechRecognitionCallback(SpeechRecognitionCallback), an internal speech recognizer will be used for which your application will need to request android.permission.RECORD_AUDIO.

So you need to do either

  • Implement setSpeechRecognitionCallback
  • Request android.permission.RECORD_AUDIO on AndroidManifest.xml

as follwing.


Overriding onSearchRequeseted to activate in-app search

When user tries voice input search, onSearchRequested callback is executed and Google’s global contents search will be launched as default.


Voice search works with returning the result of Google’s default voice search.

It is necessary to override this method if you want to activate in-app application search.

<Ref> It is written in the description of startSearch method that 

It is typically called from onSearchRequested(), either directly from Activity.onSearchRequested() or from an overridden version in any given Activity. If your goal is simply to activate search, it is preferred to call onSearchRequested(), which may have been overridden elsewhere in your Activity. If your goal is to inject specific data such as context data, it is preferred to override onSearchRequested(), so that any callers to it will benefit from the override.


We override onSearchRequested method for both MainActivity & SearchActivity.


Customize in-app search – SearchResultProvider

SearchResultProvider intereface is an intereface of Leanback library, used to listen search related event.  We need to override 3 methods.

  • getResultsAdapter – returns the adapter which includes the search results, to show search results on SearchFragment.
  • onQueryTextChange – event listener which is called when user changes search query text.
  • onQueryTextSubmit – event listener which is called when user submitted search query text. 

We need to register this SearchResultProvider by using setSearchResultProvider method,  minimum implementation is like this,


Build and run

SearchActivity can be launched in 2 ways,

  1. Explicitly click search icon of BrowseFragment.
  2. When user starts voice input search from specific controller *1 (onSearchRequested will be called.)

*1 It is depending on the Android TV devices. For example, SONY BRAVIA provides touchpad remote controller and voice search can be done from this remote controller.


SONY’s one flick touchpad remote (cited from

SearchFragment shows the mock search results now.


SearchFragment with Search results.

Source code is on github.


[Update on 1. 8. 2016]: I’m sorry that it seems the implementation is insufficient. We should implement  OnItemViewClickedListener to define the action of clicking these search results. This OnItemViewClickedListener can be set it by 

(in this case) in onCreate of BrowseFragment. Please refer Kim Johnsson’s helpful comment below.


Google’s AOSP implementation is explained in the next chapter.


Sponsored Links

4 responses

  1. Thank you so much sir it’s a very helpful tutorial, but i wonder how could i implement the OnClickItemListener method on these result. Thanks again really appreciate!!

  2. Excellent article, thanks! Finally gonna be able to add voice search to my app.

    You did miss one important part though: Implementing the onActivityResult callback in the SearchFragment. Without this, voice search will appear to work but return nothing. Took me a while to figure that one out, eventually I looked at the googlesamples example (I probably should’ve started there).

    Parinya (and others wondering): Implementing a click listener for the items is done the same way as you would in, say, a BrowseFragment. For example:

    private final class ItemViewClickedListener implements OnItemViewClickedListener {
    public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
    RowPresenter.ViewHolder rowViewHolder, Row row) {

    if (item instanceof Video) {
    Video video = (Video) item;
    //Do stuff here

    and in onCreate: setOnItemViewClickedListener(new ItemViewClickedListener());

    I’d recommend you check out Google’s here:

    • Hi Kim,
      Thank you for your update information.
      I’m sorry that I recently could not manage time to work on this, but this is very helpful information to others.

Leave a Reply

Your email address will not be published.