How to Get Loaded Web Page Title in Android WebView?

If you need to fetch the title of the webpage displayed in a WebView inside an Android app then there are two ways to do it.

You can get the webpage title using WebChromeClient in lesser time than using WebViewClient.

webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
    setTitle(title);
}
});

But there’s an issue with using onReceivedTitle. It doesn’t get invoked on goBack. So the alternative is to use the onPageFinished method of WebViewClient.

You’ll have to use a custom WebViewClient to get this done. You will override the onPageFinished() method so when a new page finishes loading you can set the webview to the appropriate title.

WebView mWebView = (WebView) findViewById(R.id.mwebview);
mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        setTitle(view.getTitle());
    }
});

This content has been helpful to you?

Thanks for contributing!

Yes No