How to Get Loaded Web Page Title in Android Webview

How to get loaded web page title in Android WebView?

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

getTitle() using web URL, rather than webView.getTitle();

Just using the URL, you'd have to load the document over the network, parse it, and then take the title—which you probably don't want to do yourself.

I think what you actually need is this: set a custom WebViewClient for your WebView, and implement onPageFinished() for that; the WebView instance passed to that method has title set. This answer has a complete example.

How detect change title of single view page in webview

You can use below code:

 mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
cardTitle.setText(title);
}

});

How to get the title of the webpage that the webview is currently holding?

Here's a full example I put together using the OnPageFinished override in a custom WebViewClient.

WebViewCustomActivity.cs

using System;
using Android.App;
using Android.OS;
using Android.Webkit;

namespace XamdroidMaster.Activities {

[Activity(Label = "Custom WebViewClient", MainLauncher = true)]
public class WebViewCustomActivity : Activity {

protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);

SetContentView(Resource.Layout.WebView);
WebView wv = FindViewById<WebView>(Resource.Id.webviewMain);

CustomWebViewClient customWebViewClient = new CustomWebViewClient();
customWebViewClient.OnPageLoaded += CustomWebViewClient_OnPageLoaded;

wv.SetWebViewClient(customWebViewClient);
wv.LoadUrl("https://www.stackoverflow.com");
}

private void CustomWebViewClient_OnPageLoaded(object sender, string sTitle) {
Android.Util.Log.Info("MyApp", $"OnPageLoaded Fired - Page Title = {sTitle}");
}

}

public class CustomWebViewClient : WebViewClient {

public event EventHandler<string> OnPageLoaded;

public override void OnPageFinished(WebView view, string url) {
OnPageLoaded?.Invoke(this, view.Title);
}

}

}

WebView.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<WebView
android:id="@+id/webviewMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</LinearLayout>

Get the Title for an Html page from WebView History after goBack()?

Apparently, this is a bug in certain builds of Android and used to work correctly.


I had been using the suggestion from https://stackoverflow.com/a/12154530/90236, but I think the answer for me is to not use onReceivedTitle() because it isn't called after a goBack.

onPageFinished() in the WebViewClient seems to be better if you need to handle goBack().

This seems to be working:

public class MyActivity extends Activity {
WebView webView;
TextView title;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

webView = (WebView) findViewById(R.id.webView);
title = (TextView) findViewById(R.id.title);
webView.loadUrl("file:///android_asset/pageone.html");

webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
title.setText(String.format("Title: %s", view.getTitle()));
}
});

final Button button = (Button) findViewById(R.id.backButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
}
}
});
}

}

However, this changes the timing of when the Android app is notified of the title. What I have found is:

  1. WebChromeClient.onReceivedTitle occurs first. However, this event does not occur when webView.goBack() is used to navigate to a page.
  2. Then the html page loads and the DOM window.onload occurs
  3. Finally WebViewClient.onPageFinished occurs

If you use Javascript to update the page title, using onPageFinished may have quite different behavior than onReceivedTitle because of the event timing.

Set page title of WebView from fragment

Change onBrowserSetTitle to have the following definition:

@Override
public void onBrowserSetTitle(String title, View view) {
if (mViewPager.getCurrentItem() == mViewPager.getAdapter().getItemPosition(view)) {
getSupportActionBar().setTitle(title);
}
}

And then call onBrowserSetTitle from onPageFinished like so:

@Override
public void onPageFinished(WebView view, String url) {
if (view.getTitle() != null && view.getTitle().length() > 0) {
mBrowserSetTitle.onBrowserSetTitle(mTitle, view);
}
}

Android Webview onReceivedTitle not called on page reload

Seems like this is a bug in chromium. I worked around this by injecting my scripts on onPageFinished method of WebviewClient.



Related Topics



Leave a reply



Submit