Mitesh Shah
Mitesh Shah

Reputation: 1739

Android - Detecting WebView URL not working

I am developing an app, in that I want to detect each time when a new url gets loaded in webview and then display that url in edittext.

I tried many solution and googled. But, not found significant solution which solves my purpose..

My Code is:

    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient()
    {
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {


            String url_new = wv.getUrl();

            Log.v("","Webview URL: "+url_new);

            edittext.setText("http://"+url_new);        

            return true;                 
        }
    }); 

Not even printing the Log Statement :(

Please Help..!! Thanks in advance..!! :)

Upvotes: 0

Views: 1462

Answers (4)

marcin.kosiba
marcin.kosiba

Reputation: 3231

First off the code you're posting is returning true from shouldOverrideUrlLoading which means you've just prevented the webview from navigating to any location.

Second off, shouldOverrideUrlLoading will not get called for any urls you pass to the webview.loadUrl API. As suggested in one of the other answers, onPageStarted is the simplest API to use for this.

You can use the code below to play around with the APIs:

wv.setWebViewClient(new WebViewClient()
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        Log.v("","Webview shouldOverrideUrlLoading URL: " + url);
        return false;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon){
        Log.v("","Webview onPageStarted URL: " + url);
    }
}); 

Upvotes: 1

Apurv
Apurv

Reputation: 333

Set WebViewClient to your webview and geturl on when page finshed laoding

    class MyWebView extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {

        editText.setText(view.getUrl());
    }

}

set this WebViewClient to your webview ..

    webView.setWebViewClient(new MyWebView());

Upvotes: 1

anthonycr
anthonycr

Reputation: 4186

This is the code I use in my applications.

WebView view = (WebView) findViewById(R.id.myWebView);
EditText edit = (EditText) findViewById(R.id.myEditText);
view.setWebViewClient(new WebViewClient(){
    public void onPageStarted (WebView view, String url, Bitmap favicon){
        edit.setText(url);
    }
});

Upvotes: 1

amukhachov
amukhachov

Reputation: 5900

You can override onLoadResource() method of WebViewClient :

 wv.setWebViewClient(new WebViewClient() {
     public void onLoadResource(WebView view, String url) {
        edittext.setText("http://"+url_new);
    }
 }

Upvotes: 0

Related Questions