androidandroid-webviewandroid-video-player

Status bar is back (visible) when controls of videoplayer in fullscreen are shown (visible)


I use WebView for loading a video. I overrided onShowCustomView (to go to fullscreen) and onHideCustomView (to get out of fullscreen) methods. When I go to fullscreen mode a variable fullscreen is set to true and OnSystemUiVisibilityChangeListener calls GoFullscreen() function that sets SYSTEM_UI_FLAG_FULLSCREEN flag and also hides Action Bar:

decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN); getSupportActionBar().hide();

But when I go to fullscreen mode status bar is visible if video controls are visible. So status bar hides only if video controls are gone (not visible).

Screenshots to see a problem with controls of video and statusbar:

------------normal (ok)

normal mode

-----fullscreen with video controls (not ok) -------- fullscreen without video controls (ok) enter image description here enter image description here

Code:

public class TestActivity 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;
private Boolean Fullscreen = false;

private void GoFullscreen() {
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
    getSupportActionBar().hide();
}

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

    decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            if (Fullscreen) {
                GoFullscreen();
            } else {
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                getSupportActionBar().show();
            }
        }
    });

    mWebView = (WebView) findViewById(R.id.webView);
    mWebView.setBackgroundColor(Color.BLACK);
    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("http://vk.com/video_ext.php?oid=-89326201&id=171643607&hash=2b18b090e13222dc&hd=1");
    }
}

@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 {
    @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;
        }
        Fullscreen = true;
        FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mContentView = (LinearLayout) findViewById(R.id.activity_main);
        mCustomViewContainer = new FrameLayout(TestActivity.this);
        mCustomViewContainer.setLayoutParams(LayoutParameters);
        mCustomViewContainer.setBackgroundResource(android.R.color.black);
        view.setLayoutParams(LayoutParameters);
        mCustomViewContainer.addView(view);
        mCustomView = view;
        mCustomViewCallback = callback;
        mContentView.setVisibility(View.GONE);
        GoFullscreen();
        mCustomViewContainer.setVisibility(View.VISIBLE);
        setContentView(mCustomViewContainer);
    }

    @Override
    public void onHideCustomView() {
        if (mCustomView != 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);
            mCustomViewContainer = null;
            mCustomViewCallback.onCustomViewHidden();
            // Show the content view.
            Fullscreen = false;
            mContentView.setVisibility(View.VISIBLE);
            setContentView(mContentView);
        }
    }
}

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

}

How to solve it?

p.s. build.gradle API: compileSdkVersion 23 / buildToolsVersion "23.0.1" / minSdkVersion 19 / targetSdkVersion 23

UPDATE: seems to be problem only on KitKat. I've tested on API 23 smartphone and it doesn't have this problem. Still need to fix it for KitKat devices


Solution

  • Eventually I decided that I should not use OnSystemUiVisibilityChangeListener (it would work buggy) and just call FullscreenOn in onShowCustomView and FullscreenOff in onHideCustomView. This would be working good, but still doesn't hide KitKat's sidebar fully (but works good for other versions of Android)

        private void FullscreenOn() {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            getSupportActionBar().hide();
        }
    
        private void FullscreenOff() {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            getSupportActionBar().show();
        }