androidx.mediarouter.app.MediaRouteButton throwing android.view.InflateException because of translucent background

438 views Asked by At

I have been working on a flutter plugin for the last few days. I am trying to implement and existing media player into a flutter widget. However since the media player's SDK uses MediaRouteButtons in the player view I have been getting an android.view.InflateException when trying to inflate it.

The core reason is because the background cannot be translucent. I have tried to set the main activity's colorPrimary through custom themes or using built in themes with opaque backgrounds. This has no effect and the error is persistant.

I suspect that flutter is adding its own theme somewhere in the mix and this is causing the issue, but I couldn't find any helpful information on that.

The SDK itself is not the problem since I am testing the same functionality with an empty view with only a MediaRouteButton. Here is my layout:

<androidx.mediarouter.app.MediaRouteButton
        android:id="@+id/media_route_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

I am creating a PlatformView and in the constructor I am trying to inflate the view. This is the constructor and the LayoutInflater is the one throwing the exception.

FlutterPlayerView(Context context, BinaryMessenger messenger, int id) {
        this.channel = new MethodChannel(messenger, CHANNEL + id);
        this.channel.setMethodCallHandler(this);
        View view = LayoutInflater.from(context).inflate(R.layout.button_view, null);
        this.buttonView = view.findViewById(R.id.media_route_button);
    }

This is a custom theme I have tried using

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">#FFFFFFFF</item>
    </style>
</resources>

I set it in the plugin manifest and also in the app manifest, and even in the generated MainActivity.java activity by using setTheme().

Nothing seems to help, so any leads would be great. If someone understands how flutter sets the themes for the plugin Activity an explanation would be greatly appreciated.

1

There are 1 answers

0
Kotesitory On BEST ANSWER

I managed to resolve the issue by setting the theme of the activity from the context passed into my PlatforView constructor

Before

FlutterPlayerView(Context context, BinaryMessenger messenger, int id) {
    this.channel = new MethodChannel(messenger, CHANNEL + id);
    this.channel.setMethodCallHandler(this);
    View view = LayoutInflater.from(context).inflate(R.layout.button_view, null);
    this.buttonView = view.findViewById(R.id.media_route_button);
}

After

FlutterPlayerView(Context context, BinaryMessenger messenger, int id) {
    this.channel = new MethodChannel(messenger, CHANNEL + id);
    this.channel.setMethodCallHandler(this);
        
    // android:colorPrimary is opaque in AppTheme
    context.setTheme(R.style.AppTheme); 
    View view = LayoutInflater.from(context).inflate(R.layout.button_view, null);
    this.buttonView = view.findViewById(R.id.media_route_button);
}

Also there is a way to do it by implementing the ActivityAware interface in your Flutter plugin and overriding the onAttachedToActivity and onRettachedToActivity methods.

For example:

public class YourFlutterPlugin implements FlutterPlugin, ActivityAware{
    //...

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        Context context = binding.getActivity();
        // Your code for sending the context to the view
        // Example:  platformView.setContext(context);
    }

    @Override
    public void onReattachedToActivity(@NonNull ActivityPluginBinding binding) {
        Context context = binding.getActivity();
        // Your code for sending the context to the view
        // Example:  platformView.setContext(context);
    }

  // ...
}