Skip to content or footer

Changing the Chromecast icon color by styling MediaRouteButton

Date

30 September 2016

Changing the Chromecast icon color by styling MediaRouteButton

Ever since Chromecast has been added as a standard functionality to mobile applications, designers want to apply a brand color to the standard cast icon.

The only way (without the support library) is to copy assets from mediarouter library, edit as needed and override the default assets or style.

While a simple fix, it’s time consuming and over engineering for a simple color change.

Android developers stopped hacking with overrides when the v7 mediarouter library added setRemoteIndicatorDrawable to the MediaRouteButton.

The second step on the right path is the setTint method, the problem is the MediaRouteButton’s drawable is not accessible in the standard way.

But it can be done, if one knows the inner workings of the support library:

Context castContext = new ContextThemeWrapper(context, android.support.v7.mediarouter.R.style.Theme_MediaRouter);

Drawable drawable = null;
TypedArray a = castContext.obtainStyledAttributes(null,
        android.support.v7.mediarouter.R.styleable.MediaRouteButton, android.support.v7.mediarouter.R.attr.mediaRouteButtonStyle, 0);
drawable = a.getDrawable(
        android.support.v7.mediarouter.R.styleable.MediaRouteButton_externalRouteEnabledDrawable);
a.recycle();

Put the two together and a simple, no hacks, no custom components solution emerges:

Context castContext = new ContextThemeWrapper(context, android.support.v7.mediarouter.R.style.Theme_MediaRouter);

Drawable drawable = null;
TypedArray a = castContext.obtainStyledAttributes(null,
        android.support.v7.mediarouter.R.styleable.MediaRouteButton, android.support.v7.mediarouter.R.attr.mediaRouteButtonStyle, 0);
drawable = a.getDrawable(
        android.support.v7.mediarouter.R.styleable.MediaRouteButton_externalRouteEnabledDrawable);
a.recycle();

Check out the full demo, with a color picker and Toolbar menu cast icon on:

https://github.com/andrei-egeniq/android-tibits/tree/master/CastColor 

Need help with Android UI customization?

Dawn Technology helps you solve Android challenges the right way. Clean code, no hacks, predictable results.