4

In my webview videos aren't able to be displayed in fullscreen. The chrome console shows the following error when I press the fullscreen button on e.g an youtube video.

Uncaught (in promise) TypeError: fullscreen error

console error

System

Android 10
Chrome version: 83.0.4103.106

App configuration

compileSdkVersion 28
defaultConfig {
    minSdkVersion 22
    targetSdkVersion 28
}

webviewSettings.setJavaScriptEnabled(true);
webviewSettings.setDomStorageEnabled(true);
webviewSettings.setSupportMultipleWindows(false);
webviewSettings.setJavaScriptCanOpenWindowsAutomatically(false);

The error is related to this bug Bug 945287 however it should be fixed in my version. Are there any other settings required to display embedded videos in fullscreen in a webview?

vato
  • 409
  • 1
  • 3
  • 15

2 Answers2

5

I found a answer for this that worked well Playing HTML5 video on fullscreen in android webview , but for me it broke the keyboard after hiding the custom view. Input text boxes wouldn't trigger the keyboard to open. The code below is all you need to do fullscreen and fix the issue I got.

MainActivity:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity
{
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new MyChrome());
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.google.com");
     }    
private class MyChrome extends WebChromeClient
{
    View fullscreen = null;

    @Override
    public void onHideCustomView()
    {
        fullscreen.setVisibility(View.GONE);
        webView.setVisibility(View.VISIBLE);
    }
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback)
    {
        webView.setVisibility(View.GONE);

        if(fullscreen != null)
        {
            ((FrameLayout)getWindow().getDecorView()).removeView(fullscreen);
        }

        fullscreen = view;
        ((FrameLayout)getWindow().getDecorView()).addView(fullscreen, new FrameLayout.LayoutParams(-1, -1));
        fullscreen.setVisibility(View.VISIBLE);
    }
}

AndroidManifest

<activity
  android:name=".MainActivity"
  android:configChanges="orientation|screenSize" />
  • For those who have faced troubles with back button - when video in fullscreen is continue to play while web view is has been closed don't forget to add view.requestFocus() in onShowCustomView – bene25 Jun 25 '23 at 13:27
4

Setting the custom WebChromeClient as posted here solved the problem.

Playing HTML5 video on fullscreen in android webview

vato
  • 409
  • 1
  • 3
  • 15