Unable to get current URL on click event of WebView in Android
private WebView wv;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.infoweblinkview);
开发者_JAVA百科 wv=(WebView) findViewById(R.id.weblinkview);
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(getString(R.string.infourl));
wv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String previousUrl = wv.getUrl();
}
});
}
for that, your can override some method in your WebViewClient
class
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
Use any of this method as per your requirement, the String url
gives you the url
The method shouldOverrideUrlLoading() will be called only when a new URL is about to load in the current webview. Hence to receive an updated URL upon click inside a single webpage application you need to extend WebChromeClient class and set its instance to your webview. Whenever URL will update onProgressChanged will be called and there you will get updated URL through webview.
webView?.webChromeClient = QuartzWebChromeClient()
Inside onProgressChanged() of QuartzWebChromeClient updated URL can be received through webview.
private inner class QuartzWebChromeClient : WebChromeClient() {
override fun onProgressChanged(view: WebView, newProgress: Int) {
super.onProgressChanged(view, newProgress)
Log.d("tag",view.url)
}
}
Like @Fatal Exception says, it´s a good option:
_browserWebview?.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView, newProgress: Int) {
super.onProgressChanged(view, newProgress)
println("URL WEBVIEW: ${view.url}")
}
}
精彩评论