Android TV and Google Cast development

This content is taken from the corresponding udacity course.

Table of Contents

  1. Living Room
  2. Google Cast
  3. Android TV

Living Room

Average TV-Viewer spends 3h/day!

First extend you app with Google Cast Support, then extend with leanback library to allow users to control your app from Android TV.

Google Cast

Send = cast to Receiver (Android TV, Chromecast). Senders are Android, iOS, Web.

Google Cast is a connecting technologie.

Use the Cast SDK, to make your App a Sender.

The Cast SDK creates a menu item with cast icon. With it you can establish a connection with your Cast device.

Explore the sample apps to learn about Google Cast.

Become a Google Cast Developer costs 5$ and can be accomplished in the Google Cast SDK Developer Console. Then you can register your receiver Applications and your cast devices = device’s serial number.

Cast isn’t supported by the emulator! Use a real Android and a real Cast device for development.

Follow the Google’s visual design checklist.

Use Cast Companion Library (CCL).

Provide start and stop control casts for users at any time. Always show playback controls when casting (ex. minicontroler or fullscreen). Even show playback controls, when the device is locked and over notifications while you app is out of focus. Also user should always be able to disconnect from the device.

If multiple users are connected, only stop the actual cast when the last user disconnects.

If the connection is lost, automatically reconnect users when they’re in your app.

If your app is killed, always try to rejoin the existing session, if it’s still running.

2 ways to start casting:

The Sender always show the action. The Receiver always shows the state. Don’t confuse your users! Think at remote control and TV.

Fade your Receiver UI away after 5 seconds.


They are written in HTML5 and JavaScript.

Receivers get an URL generated from the Application ID by the chrome cast. The Sender sends the Application ID to the chrome cast.

Types of receiver applications:


The sender’s lifecycle: Parto Karwat

  1. Manifest
    • minSdkVersion >= 9
    • set the correct Application Theme
    • Add ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE in the production app
  2. menu.xml
    • Add the Cast button
  3. Activiy
    • Initialize the Cast API in onCreate()
    • Assign the the MediaRouteSeletor to MediaRouteActionProvider in onCreateOptionsMenu()
    • Add MediaRouterCallback to the MediaRouter instance in onStart()
    • Remove MediaRouterCallback in onStop() to conserve battery power

Example Cast API initialization:

 mMediaRouter = MediaRouter.getInstance(getApplicationContext());
 mMediaRouteSelector = new MediaRouteSelector.Builder().addControlCategory(CastMediaControlIntent.categoryForCat("794B7BBF")).build();
 mMediaRouterCallback = new MyMediaRouterCallback();

Code for Cast button menu item:

 	android:title="Play on..."

Again use CCL (Cast Companion Library)!

Try out a cast codelab! :)

Android TV

Use leanback library, it does the most.

Use TV emulator, it’s possible. :)

In your manifest:

Use RecyclerView because of large list and limited memory on your Android TV.