Cezar Alexandru Vancea
Cezar Alexandru Vancea

Reputation: 580

Android WebView issue on override url loading

I’m trying to override url loading when clicking on a link on an app WebView.

The page loads but the WebView will keep it’s last scroll position and content size.

Is there some parameter I forgot to set on the WebView to reset the content size and the scroll position on next load?

Here’s how I’m using it:

@SuppressLint("SetJavaScriptEnabled")
@AfterViews
protected void afterViews() {
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new WebClient());
  webView.setWebChromeClient(new ChromeClient());
  webView.loadUrl(url);
}
public class WebClient extends WebViewClient {
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    boolean isValidEmail = url.startsWith("mailto:") && Patterns.EMAIL_ADDRESS.matcher(url.substring("mailto:".length())).matches();
    boolean isValidPhone = url.startsWith("tel:") && Patterns.PHONE.matcher(url.substring("tel:".length())).matches();
    if (url.startsWith("about:")) {
      return super.shouldOverrideUrlLoading(view, url);
    }
    if (isValidEmail) {
      Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
      startActivity(Intent.createChooser(sendIntent, ""));
    } else {
      if (isValidPhone) {
        Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
        startActivity(Intent.createChooser(dialIntent, ""));
      } else {
        WebViewActivity.this.setPageTitle(url);
        webView.loadUrl(url);
      }
    }
    return true;
  }
  @Override
  public void onPageFinished(WebView view, String url) {
    //..
  }
}

public class ChromeClient extends WebChromeClient {
  @Override
  public void onProgressChanged(WebView view, int progress) {
    //...
  }
}

Thanks.

Upvotes: 1

Views: 3083

Answers (2)

Cezar Alexandru Vancea
Cezar Alexandru Vancea

Reputation: 580

@Bojan Kopanja, my sincere apologies for misleading. It turns out I had the webview inside a pull to refresh listener with a scrollview and removing that got rid of the problem. Thanks for your help nevertheless.

Upvotes: 0

Bojan Kopanja
Bojan Kopanja

Reputation: 734

Did you try to set scroll position in onPageFinished?

@Override
public void onPageFinished(WebView view, String url) {
    // other code...
    view.scrollTo(0,0);
}

That should set WebView content back to top, and not on old scroll position

Edit 1:

Sometimes if the page loads for a long time this won't work properly, so we must wait for page to get fully loaded:

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

Please note that webView this time is not the WebView from the method onPageFinished, but the WebView fetched from the layout (same one as in afterViews() method).

Upvotes: 1

Related Questions