I have an example code that creates WebView for HTML5 video and it allows 
to enable fullscreen (actually there is also a problem with hiding status 
bar, but it's not the main problem now).

public class MainActivity extends AppCompatActivity {

    private MyWebChromeClient mWebChromeClient = null;
    private View mCustomView;
    private LinearLayout mContentView;
    private FrameLayout mCustomViewContainer;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    private Bundle webViewBundle;
    private WebView mWebView;
    private int viewWidth;
    private int viewHeight;
    private LinearLayout.LayoutParams layoutParams;
    private View decorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        decorView = getWindow().getDecorView();

        mWebView = (WebView) findViewById(R.id.webView);
        mWebChromeClient = new MyWebChromeClient();
        mWebView.setWebChromeClient(mWebChromeClient);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        viewWidth = 480;
        viewHeight = (int) (((double) viewWidth) * 0.5625); // video aspect 16:9
        layoutParams = new LinearLayout.LayoutParams(viewWidth, viewHeight);
        mWebView.setLayoutParams(layoutParams);

        if (webViewBundle != null) {
            mWebView.restoreState(webViewBundle);
        } else {
            mWebView.loadUrl(%VIDEO_URL%); // e.g, page with iframe video
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        webViewBundle = new Bundle();
        mWebView.saveState(webViewBundle);
        mWebView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
    }

    public class MyWebChromeClient extends WebChromeClient {

        FrameLayout.LayoutParams LayoutParameters = new 
FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
FrameLayout.LayoutParams.MATCH_PARENT);

        @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;
            }
            mContentView = (LinearLayout) findViewById(R.id.activity_main);
            mContentView.setVisibility(View.GONE);
            mCustomViewContainer = new FrameLayout(MainActivity.this);
            mCustomViewContainer.setLayoutParams(LayoutParameters);
            mCustomViewContainer.setBackgroundResource(android.R.color.black);
            view.setLayoutParams(LayoutParameters);
            mCustomViewContainer.addView(view);
            mCustomView = view;
            mCustomViewCallback = callback;
            getSupportActionBar().hide();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            mCustomViewContainer.setVisibility(View.VISIBLE);
            setContentView(mCustomViewContainer);
        }

        @Override
        public void onHideCustomView() {
            if (mCustomView == null) {
                return;
            } else {
                // Hide the custom view.
                mCustomView.setVisibility(View.GONE);
                // Remove the custom view from its container.
                mCustomViewContainer.removeView(mCustomView);
                mCustomView = null;
                mCustomViewContainer.setVisibility(View.GONE);
                mCustomViewCallback.onCustomViewHidden();
                // Show the content view.
                int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
                decorView.setSystemUiVisibility(uiOptions);
                getSupportActionBar().show();
                mContentView.setVisibility(View.VISIBLE);
                setContentView(mContentView);
            }
        }
    }
    @Override
    public void onBackPressed() {
        if (mCustomViewContainer != null)
            mWebChromeClient.onHideCustomView();
        else if (mWebView.canGoBack())
            mWebView.goBack();
        else
            super.onBackPressed();
    }}

This code for the activity. But I want to use it in fragments (because they 
have a power and I can use them for ViewPager). I could not find any 
example with overriding `onShowCustomView, onHideCustomView, onBackPressed' 
in fragments.

Here what I did for a fragment

public class TestFragment extends Fragment {

    private static final String SOME_ID = "blabla";

    private int viewWidth;
    private int viewHeight;
    private View v;
    private LinearLayout.LayoutParams layoutParams;
    private Episode mEpisode;
    private WebView mWebView;
    private Bundle webViewBundle;
    private MyWebChromeClient mWebChromeClient = null;
    private View mCustomView;
    private FrameLayout mCustomViewContainer;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    private View decorView;

    public static TestFragment newInstance(UUID blabla) {
        Bundle args = new Bundle();
        args.putSerializable(SOME_ID, blabla);
        TestFragment fragment = new TestFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_episode, container, false);

        decorView = getActivity().getWindow().getDecorView();
        mWebView = (WebView) v.findViewById(R.id.webView);
        mWebChromeClient = new MyWebChromeClient();
        mWebView.setWebChromeClient(mWebChromeClient);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        viewWidth = 400;
        viewHeight = (int) (((double) viewWidth) * 0.5625); // webview size for 
16:9 videos
        mWebView.setLayoutParams(new LinearLayout.LayoutParams(viewWidth, 
viewHeight));
        if (webViewBundle != null) {
            mWebView.restoreState(webViewBundle);
        } else {
            mWebView.loadUrl(%VIDEO_URL%); // e.g, page with iframe video
        }

        return v;
    }

    @Override
    public void onPause() {
        super.onPause();
        webViewBundle = new Bundle();
        mWebView.saveState(webViewBundle);
        mWebView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
    }

    public class MyWebChromeClient extends WebChromeClient {
        FrameLayout.LayoutParams LayoutParameters = new 
FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
FrameLayout.LayoutParams.MATCH_PARENT);

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            // if a view already exists then immediately terminate the new one
            if (mCustomView != null || v == null) {
                callback.onCustomViewHidden();
                return;
            }
            v.setVisibility(View.GONE);
            mCustomViewContainer = new FrameLayout(getActivity());
            mCustomViewContainer.setLayoutParams(LayoutParameters);
            mCustomViewContainer.setBackgroundResource(android.R.color.black);
            view.setLayoutParams(LayoutParameters);
            mCustomViewContainer.addView(view);
            mCustomView = view;
            mCustomViewCallback = callback;
            ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            mCustomViewContainer.setVisibility(View.VISIBLE);
            getActivity().setContentView(mCustomViewContainer);
        }

        @Override
        public void onHideCustomView() {
            if (mCustomView != null && v != null) {
                // Hide the custom view.
                mCustomView.setVisibility(View.GONE);
                // Remove the custom view from its container.
                mCustomViewContainer.removeView(mCustomView);
                mCustomView = null;
                mCustomViewContainer.setVisibility(View.GONE);
                mCustomViewCallback.onCustomViewHidden();
                // Show the content view.
                int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
                decorView.setSystemUiVisibility(uiOptions);
                ((AppCompatActivity)getActivity()).getSupportActionBar().show();
                v.setVisibility(View.VISIBLE);
                getActivity().setContentView(v);
            }
        }
    }}

I can go to fullscreen using this code (onShowCustomView works good), but I 
can't get out of fullscreen because I can't use next code in fragments

The app just crashes if I press back button

@Override
    public void onBackPressed() {
        if (mCustomViewContainer != null)
            mWebChromeClient.onHideCustomView();
        else if (mWebView.canGoBack())
            mWebView.goBack();
        else
            super.onBackPressed();
    }

Any ideas?

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/ee21a7e1-eccf-4970-bf5a-ada77e7e64be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to