I have a Toolbar
on the Activity which includes cast icon.
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
CastManager.getInstance(this)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.activity_menu, menu)
CastManager.setupCastButtonIfSupported(this, menu)
}
}
And the menu xml is
<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="Cast"
app:actionProviderClass="CustomMediaRouteActionProvider"
app:showAsAction="always"/>
</menu>
And the cast icon looks like this.
The cast icon here is picking up mediaRouteButtonTint
colour defined in themes.xml
. The challenge is to set tint colour programmatically. I tried using custom ActionProvider
class to apply tinting.
class CustomMediaRouteActionProvider(context: Context) : MediaRouteActionProvider(context) {
override fun onCreateMediaRouteButton(): MediaRouteButton {
val button = super.onCreateMediaRouteButton()
addTinting(button)
return button
}
override fun getMediaRouteButton(): MediaRouteButton? {
val button = super.getMediaRouteButton()
addTinting(button)
return button
}
private fun addTinting(button: MediaRouteButton?) {
if(null == button) return
val castContext: Context =
ContextThemeWrapper(context, androidx.mediarouter.R.style.Theme_MediaRouter)
val a = castContext.obtainStyledAttributes(
null,
androidx.mediarouter.R.styleable.MediaRouteButton,
androidx.mediarouter.R.attr.mediaRouteButtonStyle,
0
)
val drawable = a.getDrawable(androidx.mediarouter.R.styleable.MediaRouteButton_externalRouteEnabledDrawable)
a.recycle()
if (drawable != null) {
DrawableCompat.setTintList(drawable, customDynamicColor)
}
if (drawable != null) {
drawable.state = button.drawableState
}
button.setRemoteIndicatorDrawable(drawable)
}
}
However, it is not changing tinting. I'm not sure what is going wrong here. Any clue on what's breaking?