Michal_196 Michal_196 - 6 months ago 146
Java Question

Why does full screen button not appearing in webview activity

I have enabled VideoEnabledWebView in my fragment still, no full screen button appears on webview while watching video, what's the cause?

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

String url = "http://youtube.com";

myWebView = (VideoEnabledWebView) rootView.findViewById(R.id.webViewTop);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.getSettings().setJavaScriptEnabled(true);
progress = (ProgressBar) rootView.findViewById(R.id.progressBar3);
View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout);
ViewGroup videoLayout = (ViewGroup)rootView.findViewById(R.id.videoLayout);
setHasOptionsMenu(true);
webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, myWebView);

myWebView.loadUrl(url);

webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
{
@Override
public void toggledFullscreen(boolean fullscreen)
{

if (fullscreen)
{
WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getActivity().getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
//noinspection all
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}
else
{
WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getActivity().getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
//noinspection all
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}

}

});

myWebView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView View, String url) {
View.loadUrl(url);
TopRatedFragment.this.progress.setProgress(0);

return true;
}
});
myWebView.setOnKeyListener(new android.view.View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;

switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}

return false;
}
});
return rootView;

}
}


Here is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<RelativeLayout
android:id="@+id/nonVideoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="3dip"
android:max="100"
android:progressDrawable="@drawable/greenprogress"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<com.taadu.slidechat.adaptor.VideoEnabledWebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webViewTop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/progressBar3"/>

</RelativeLayout>
<RelativeLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</RelativeLayout>


Searched hours, still could not find anything related to enabling full screen button,

please help.

UPDATE

Adding this library
https://github.com/delight-im/Android-AdvancedWebView
brings full screen button on videos, but after clicking the full screen button, it hangs all together.

Answer

Fixed it

I was not calling videoenabledchromeclient, that's why it was not going in full screen mode. Here is the fixed code;

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

    String url = "http://youtube.com";
    //layout
    myWebView = new VideoEnabledWebView(getActivity());
    myWebView = (VideoEnabledWebView) rootView.findViewById(R.id.webViewTop);
    mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefresh);
    progress = (ProgressBar) rootView.findViewById(R.id.progressBar3);
    View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout);
    ViewGroup videoLayout = (ViewGroup)rootView.findViewById(R.id.videoLayout);
    View loadingView = getActivity().getLayoutInflater().inflate(R.layout.view_loading_video, null);
webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout,  videoLayout, loadingView, myWebView)
    {
 public void onProgressChanged(WebView view, int newProgress) {

         progress.setProgress(newProgress);
         progress.setVisibility(View.VISIBLE);
          setRefreshActionButtonState(true);


          if (newProgress == 100) {
           progress.setVisibility(View.GONE);
           setRefreshActionButtonState(false);
              mSwipeRefreshLayout.setRefreshing(false);


             }
           }
         };

webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
    {
        @Override
        public void toggledFullscreen(boolean fullscreen)
        {
            // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
            if (fullscreen)
            {

                WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                getActivity().getWindow().setAttributes(attrs);
                if (android.os.Build.VERSION.SDK_INT >= 14)
                {
                    //noinspection all
                    getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                }
            }
            else
            {
                WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                getActivity().getWindow().setAttributes(attrs);
                if (android.os.Build.VERSION.SDK_INT >= 14)
                {
                    //noinspection all
                    getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                }
            }

        }
    });
    myWebView.setWebChromeClient(webChromeClient);

  return rootview;}