Search code examples
androidgoogle-chromebuttongoogle-castchromecast

Android Chrome Cast button


I'm just developing a chrome cast app and got stuck on one basic step. I'm following some examples from the Internet concerning the GUI for Chrome Cast. The button that is supposed to launch the cast (first choose a device) is normally located in the action bar. This is how it's added:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item);

    return true;
}

and XML for R.id.media_route_menu_item

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/media_route_menu_item"
        android:title="@string/media_route_menu_title"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        app:showAsAction="always"/>

</menu>

as you can see in the 2nd part:

    app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"

will start the Chrome Cast devices choice. I need to move the menu element out of action bar and make a Button which does the same. The problem is I cannot do something like

<Button
...
            app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
/>

it won't work since actionProviderClass is prepared to work with menu items. I'd be thankful for any ideas how to solve that problem using a button.


Solution

  • Check out this sample on our GitHub repository, we show three different ways to do that. If you still have questions after going through them, come back and we'll see what we can do to help.