great experiences on androidtv are casual, cinematic, and simple. you can lean back onthe couch and enjoy hundreds of movies,tv series, and videos from various sources.
Android TV legal, but wait. how can you findyour favorite movie amongst thousands ofmovies in your library? well, the answer is thatyou can just search for it.
in fact, searching is somethingthat android tv does really well. i'm wayne piekarski, adeveloper advocate at google, and today, i'm going to show youhow search works on android tv and how you can surface thegreat content of your app to your users. this here is theandroid tv home screen. users can easilyaccess the search menu and ask for anythingusing their own voice
or via keyboard input. the results are displayedin a well organized form and users have one clickaccess to launch the content. this is a really cool feature. so you might be worriedthat your users won't be able to find the contentcontained within your app. the good news is thatandroid tv search enables your app to surfaceits content outside of the app. your users will be muchhappier to easily find
what they're after, and inturn, this will drive engagement with your app. let's quickly lookat how search works. android tv will show thesearch results from google, but it also queries locallyinstalled searchable apps and combines these results. building a searchableapp on android tv is just the same as building asearchable app on other android phones and tablets, so you canuse exactly the same android
tools as what you'realready familiar with. let's talk about whatsome of the steps are to go and make this happen. first, you have to letandroid framework know that your app is searchable. this is done by adding anxml file with some metadata in an xml resourcesfolder, like this one here. so what does this filelook like, exactly? label is the nameof your app and it
should be the same as the nameapplied to the android label attribute of your activity orapplication manifest element. this label is used toidentify your application when providing searchresults and to categorize it. the search suggest authorityuniquely identifies your content provider. therefore, this value mustmatch the authority string provided in theandroid authorities attribute of the androidmanifestprovider element.
and search suggest intentaction is the default intent action to be used when a userclicks on a search result. mainly, this is a viewintent that we use here. this is a deeplink into your app and should go straightto the selected content. finally, since we want tosurface our results globally, we need to set the includeglobal search attribute value to true. for more detailed informationof other attributes,
you can always refer to thislink in the android developer documentation. next, you need to definea content provider for the search results inthe androidmanifest xml file. as i mentioned earlier,the content provider should have the exactsame authority string provided in searchsuggest authority, and it has to beallowed to access this provider from other apps.
the actual implementationof the content provider depends on you, and you canfind a step by step guide for building a provider atthe android developer site. however, today, i'vegot some tips for you to help implementing asearch result provider. since the main purpose ofthe search result provider is providing searchresults, you don't need to implement methodslike insert or update, just the query method.
all the necessaryconstant strings like uri path andcolumns which you might need toimplement a provider are already defined inthe search manager class. some results, for example,are result_card_image, image, which is used fordefining an image, poster, or cover of your media,content_type for your media's mime type, and duration,which includes time information in milliseconds.
you can also define whatdata should be passed back to your app when a userclicks on a search result by setting an intent datacolumn value or a combination of intent data idand a search suggest intent data attribute in theprevious searchable element. therefore, in thequery method, you should gather result datafrom your data source based on the querystring and then map that data into the cursorby using the pre-defined column
names. the android tv frameworkwill then take your cursor and look it up using the samecolumn names that you used and display nice lookingresult cards using data that you provide. and that's it. you can verify thisby installing your app to the device, performinga test query on android tv, and you find that the resultsfrom your app are included.
i also have aspecial tip for you. if you've had the chanceto use android tv devices, you may have noticed this. as you see here, the searchresult itself came from google, but when a userclicks this button, a specific app islaunched directly. we call this a watch actionlink, and with your app, you can take advantageof this cool feature and it's really easy to do.
when you provide datafor search results and return enoughmetadata to identify that the media fromyour app matches the results fromgoogle, then android tv will automatically attachthe watch action link on the search result card. so keep this in mind. try to provide rich andaccurate metadata for your media to assist this process.
and here's just onelast thing for you. if you would like toprovide in app search functionality in your app,this is really easy as well. just override the onsearch requested callback in your tv activity andthe default search icon will appear on the top ofyour layout, like this. whenever a userselects the search icon or clicks the search buttonon the remote control, your callback will be invoked.
inside the callback, you canlaunch your own search result activity, as shownin this example here. to make your life easier,the leanback support library provides a searchfragment widget which implements a main layoutfor the search results screen, and you can find outmore detailed information about the leanback libraryin this previous dev byte. if you want to test theconcepts i just explained, the code is available as asample at this link here.
if you want to learn more aboutother interesting android tv features like recommendations,the background manager, you should definitely check outthe android tv documentation at the androiddevelopers website. so that's it for today.
i hope you enjoyed this video. i'm wayne piekarski. thanks for watching andi'll see you next time. [music playing]