Full screen from WebView does not work

10

I've been looking for ways to make the full-screen button of the Video.JS player work from a Webview view in android.

This is the code I'm trying, it only works with youtube videos.

I've seen the full-screen button if it works from the Chrome mobile browser

Any help I will be eternally grateful

MainActivity.java

  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.webkit.WebChromeClient;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.FrameLayout;

  public class MainActivity extends Activity {
  private WebView webView;
  private FrameLayout customViewContainer;
  private WebChromeClient.CustomViewCallback customViewCallback;
  private View mCustomView;
  private myWebChromeClient mWebChromeClient;
  private myWebViewClient mWebViewClient;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    customViewContainer = (FrameLayout)    findViewById(R.id.customViewContainer);
    webView = (WebView) findViewById(R.id.webView);

    mWebViewClient = new myWebViewClient();
    webView.setWebViewClient(mWebViewClient);

    mWebChromeClient = new myWebChromeClient();
    webView.setWebChromeClient(mWebChromeClient);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSaveFormData(true);
    webView.loadUrl("http://videojs.com/");
}

public boolean inCustomView() {
    return (mCustomView != null);
}

public void hideCustomView() {
    mWebChromeClient.onHideCustomView();
}

@Override
protected void onPause() {
    super.onPause();    //To change body of overridden methods use File | Settings | File Templates.
    webView.onPause();
}

@Override
protected void onResume() {
    super.onResume();    //To change body of overridden methods use File | Settings | File Templates.
    webView.onResume();
}

@Override
protected void onStop() {
    super.onStop();    //To change body of overridden methods use File | Settings | File Templates.
    if (inCustomView()) {
        hideCustomView();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        if (inCustomView()) {
            hideCustomView();
            return true;
        }

        if ((mCustomView == null) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

class myWebChromeClient extends WebChromeClient {
    private Bitmap mDefaultVideoPoster;
    private View mVideoProgressView;

    @Override
    public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
        onShowCustomView(view, callback);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void onShowCustomView(View view,CustomViewCallback callback) {

        // if a view already exists then immediately terminate the new one
        if (mCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }
        mCustomView = view;
        webView.setVisibility(View.GONE);
        customViewContainer.setVisibility(View.VISIBLE);
        customViewContainer.addView(view);
        customViewCallback = callback;
    }

    @Override
    public View getVideoLoadingProgressView() {

        if (mVideoProgressView == null) {
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            mVideoProgressView = inflater.inflate(R.layout.video_progress, null);
        }
        return mVideoProgressView;
    }

    @Override
    public void onHideCustomView() {
        super.onHideCustomView();    //To change body of overridden methods use File | Settings | File Templates.
        if (mCustomView == null)
            return;

        webView.setVisibility(View.VISIBLE);
        customViewContainer.setVisibility(View.GONE);

        // Hide the custom view.
        mCustomView.setVisibility(View.GONE);

        // Remove the custom view from its container.
        customViewContainer.removeView(mCustomView);
        customViewCallback.onCustomViewHidden();

        mCustomView = null;
    }
}

class myWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);    //To change       body of overridden methods use File | Settings | File Templates.
    }
  }



 }

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView"
        android:layout_gravity="center"
        />
    <FrameLayout
        android:id="@+id/customViewContainer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        />
    </LinearLayout>
    
asked by JESUS ESPINOSA 17.02.2017 в 16:19
source

3 answers

4

It is very important to comment that a WebView is a view that displays web pages as if it were a browser but in reality very limited , (unlike UIWebView in iOS that can load a page without problems), in this case if you want to use a Javascript-based framework, even if you enable JavaScript execution it would actually be difficult to get all the functionality.

In order to achieve the performance of a player VideoJS within a browser and achieve the "Full Screen" mode, it would be very complicated, for this the option would have to be native.

I recommend an option that is JieCaoVideoPlayer , through this library you can play the video in "Full Screen" mode without problem , compared to other options when rotating works correctly.

    
answered by 01.03.2017 в 01:50
2

It can help you something like this:

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // remover linea de titulo
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
}}

Either by editing the manifest:

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

Source: Stackoverflow

And the third alternative (and not worse): GitHub 'MainActivity full screen'

    
answered by 25.02.2017 в 20:51
1

Try adding the following to your WebView :

webView.setWebChromeClient(new WebChromeClient() {

        public void onShowCustomView (View view, WebChromeClient.CustomViewCallback callback) {
            //Crear un toast para ver que pasa
        }

        public void onHideCustomView () {
            //Crear otro toast para ver que pasa
        }

    });

You can also see the answers to this question .

Or, I'll leave you with this question similiar in which the OP solved it looking for the word "fullscreen" in the link of the video, that way if I found that word I opened the video with the local android player.

    
answered by 17.02.2017 в 16:58