Reputation: 145
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.
Upvotes: 2
Views: 2422
Reputation: 53600
Just to complete Silva's answer this is what you need in your activity if you are using Tollbar:
MediaRouteButton mediaRouteButton = (MediaRouteButton) mToolbar.findViewById(R.id.mediaRouteMenuItem);
// mediaRouteButton.setRouteSelector(MediaRouteSelector.EMPTY);
CastButtonFactory.setUpMediaRouteButton(this, mediaRouteButton);
Upvotes: 0
Reputation: 342
You can use the MediaRouteButton like so:
XML
<android.support.v7.app.MediaRouteButton
android:id="@+id/media_route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Java
MediaRouteButton mediaRouteButton = MediaRouteButton) findViewById(R.id.media_route_button);
mediaRouteButton.setRouteSelector(your_selector);
Upvotes: 4
Reputation: 19034
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.
Upvotes: -1