Courtesy: GitHub

Android BasicMediaRouter Sample

This sample demonstrates the use of the MediaRouter API to display content on a secondary display. Use the "Media Route Action Item" in the ActionBar to select an output device. If your device supports Miracast wireless displays, you may need to enable "Wireless Display" functionality in the system settings. Secondary screen simulation can also be enabled from the "Developer Options".

Once connected, use the "Change Color" button to change the background color of the secondary screen.


  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.


If you've found an error in this sample, please file an issue:

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see for more details.


Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile ""

compile ""

compile ""

compile ""

compileSdkVersion 26

minSdkVersion 17

targetSdkVersion 26


versionCode 1

versionName 1.0


versionCode 1

versionName 1.0



This sample demonstrates the use of the MediaRouter API to show content on a secondary display using a {@link}.

The activity uses the {@link} API to automatically detect when a presentation display is available and to allow the user to control the media routes using a menu item provided by the {@link}. When a presentation display is available a {@link} (implemented as a {@link SamplePresentation}) is shown on the preferred display. A button toggles the background color of the secondary screen to show the interaction between the primary and secondary screens.

This sample requires an HDMI or Wifi display. Alternatively, the "Simulate secondary displays" feature in Development Settings can be enabled to simulate secondary displays.

* @see @see

Implementing a {@link} to update the displayed {@link} when a route is selected, unselected or the presentation display has changed. The provided stub implementation {@link} is extended and only {@link, int,} , {@link, int,} and {@link,} are overridden to update the displayed {@link} in {@link #updatePresentation()}. These callbacks enable or disable the second screen presentation based on the routing provided by the {@link} for {@link} streams. @

A new route has been selected as active. Disable the current route and enable the new one.

The route has been unselected.

The route's presentation display has changed. This callback is called when the presentation has been activated, removed or its properties have changed.

Updates the displayed presentation to enable a secondary screen if it has been selected in the {@link} for the {@link} type. If no screen has been selected by the {@link}, the current screen is disabled. Otherwise a new {@link SamplePresentation} is initialized and shown on the secondary screen.

Inflates the ActionBar or options menu. The menu file defines an item for the {@link}, which is registered here for all live video devices using {@link}.

Listens for dismissal of the {@link SamplePresentation} and removes its reference.

Displays the next color on the secondary screen if it is activate.