I'm using Google's method to add a Cast button to my app, but it's defaulted to the white icon, which is invisible against my white menu bar. How do I go about changing the color of the Cast icon to black?
I've just extended MediaRouteActionProvider like this:
public class ThemeableMediaRouteActionProvider extends MediaRouteActionProvider {
public ThemeableMediaRouteActionProvider(Context context) {
super(context);
}
@Override
public MediaRouteButton onCreateMediaRouteButton() {
MediaRouteButton button = super.onCreateMediaRouteButton();
colorWorkaroundForCastIcon(button);
return button;
}
@Nullable
@Override
public MediaRouteButton getMediaRouteButton() {
MediaRouteButton button = super.getMediaRouteButton();
colorWorkaroundForCastIcon(button);
return button;
}
private void colorWorkaroundForCastIcon(MediaRouteButton button) {
if (button == null) return;
Context castContext = new ContextThemeWrapper(getContext(), androidx.mediarouter.R.style.Theme_MediaRouter);
TypedArray a = castContext.obtainStyledAttributes(null, androidx.mediarouter.R.styleable.MediaRouteButton, androidx.mediarouter.R.attr.mediaRouteButtonStyle, 0);
Drawable drawable = a.getDrawable(androidx.mediarouter.R.styleable.MediaRouteButton_externalRouteEnabledDrawable);
a.recycle();
DrawableCompat.setTint(drawable, getContext().getResources().getColor(R.color.primary));
drawable.setState(button.getDrawableState());
button.setRemoteIndicatorDrawable(drawable);
}
}
R.color.primary is what color i wanted.
then just replace actionProviderClass in menu from MediaRouteActionProvider to your like that:
<item
android:id="@+id/media_route_menu_item"
android:title="@string/media_route_menu_title"
bwq:actionProviderClass="tv.test.playback.chromecast.ThemeableMediaRouteActionProvider"
bwq:showAsAction="always">
</item>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With