user321721
user321721

Reputation: 91

Webview goes back to page top after exit from Fullscreen video

After I close video fullscreen, I want webview to show it's last position, i.e, before going into full screen mode. Right now it's going to the top of the page and I have to scroll down again to find the video. here is my code:

private class ChromeClient extends WebChromeClient {
    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    private int mOriginalOrientation;
    private int mOriginalSystemUiVisibility;

    ChromeClient() {}


    public Bitmap getDefaultVideoPoster()
    {
        if (mCustomView == null) {
            return null;
        }

        return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
    }

    public void onHideCustomView()
    {
        ((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
        this.mCustomView = null;
       getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
        setRequestedOrientation(this.mOriginalOrientation);
        this.mCustomViewCallback.onCustomViewHidden();
        this.mCustomViewCallback = null;

    }


    public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
    {

        if (this.mCustomView != null) {
            onHideCustomView();
            return;
        }


        this.mCustomView = paramView;
        this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

       ((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
       getWindow().getDecorView().setSystemUiVisibility(3846 | SYSTEM_UI_FLAG_LAYOUT_STABLE);

    }
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    webview.saveState(outState);
}

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    webview.restoreState(savedInstanceState);
}

I can't understand why the webview is going back to the top again after I exit full screen. Please help me to fix that.

Upvotes: 4

Views: 1793

Answers (1)

Suman Dey
Suman Dey

Reputation: 57

Hi I think the code above needs only some line of code to fix that

Step 1

Declare a variable in your ChromeClient class.

int positionY;

Step 2

Now save the scroll position before going to fullscreen mode.

positionY = webView.getScrollY();

here I can see that you are going to fullscreen on onShowCustomView method so add this line under this method.

Step 3

Now restore the scroll position after coming back from fullscreen.

webView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    webView.scrollTo(0, positionY);
                }
                // Delay the scrollTo to make it work
            }, 300);

add this code under your onHideCustomView method.

That's it you are done with the coding run it and it will show the position correctly.

Upvotes: 3

Related Questions