How to get loaded web page title in Android WebView?

My observation is that, you get the webpage title using WebChromeClient in lesser time than using WebViewClient

webview.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
        if (!TextUtils.isEmpty(title)) {
            YourActivity.this.setTitle(title);
        }
    }
});

Simplest way to read title of page in Kotlin :

      webView.webViewClient = object : WebViewClient() {
         //....
          override fun onPageFinished(view: WebView, url: String) {
           val title = view.title
            }
           //...
          }

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.

Below is a sample implementation of the above:

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

You're a going to do that where you're initializing your webview. Replace the "ExperimentingActivity" to whatever you activity's name is.

If you're already overriding the WebViewClient, just add this function or the code inside to your already existing function.

You can get more info on the classes and functions I'm using here at:

Android Developers: Activity - setTitle()

Android Developers: WebViewClient

Android Developers: WebView