Search code examples
androidcastingchromecastgoogle-castgoogle-cast-sdk

How to customize Cast Dialog in the Google Cast Framework for Android


I am trying to customize the Google Cast SDK's Cast Dialog (shown when you tap the cast button and shows the list of available devices), but i haven't found a way to do it.

Currently, it just shows an AlertDialog with a list of the available devices to connect.

What i want to do instead, is open an Activity that will show the list of devices with my own UI implementation.

This is the dialog i am trying to modify:

enter image description here

I've found many customization aspects about this SDK, but nothing related to this dialog.


Solution

  • So i figured out a way to achieve this, First i created a class that overrides MediaRouteActionProvider (which is the main class that controls that button's functionality)

    public class CustomMediaRouteActionProvider extends androidx.mediarouter.app.MediaRouteActionProvider {
    
        public CustomMediaRouteActionProvider(Context context) {
            super(context);
        }
    
        @Override
        public MediaRouteButton onCreateMediaRouteButton() {
            return new CastButton(getContext());
        }
    }
    

    Then you're gonna need to override the button's functionality with your own, in my case i open a new activity.

    public class CastButton extends MediaRouteButton {
    
    
        public CastButton(Context context) {
            this(context, null);
        }
    
        public CastButton(Context context, AttributeSet attrs) {
            this(context, attrs, R.attr.mediaRouteButtonStyle);
        }
    
        public CastButton(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean performClick() {
            Intent i = new Intent(getContext(), RemoteDevicesActivity.class);
            getContext().startActivity(i);
            return true;
        }
    }
    

    Finally, you need to modify your xml that contains this button (i assume that you already implemented this part)

    Change the field app:actionProviderClass with your custom class (in this case CustomMediaRouteActionProvider) and you're done.

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <item android:title="@string/connect_to"
            android:id="@+id/cast"
            app:actionProviderClass="CustomMediaRouteActionProvider"
            app:showAsAction="ifRoom" />
    
    </menu>