Ammar
Ammar

Reputation: 1233

Get URL From WebView

I was wondering if there exits another effective way to get the URL that will load up on the WebView. My current code gives the current Url and not the one that will be loaded.

For example if my WebView load this: http://stackoverflow.com

After loading If I click on Questions, I would not get this url http://stackoverflow.com/questions, for some reason I would get http://stackoverflow.com

So my question is how would you get the url that will be loading on a WebView?

This is my code, please help!

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

    // Declaring
    WebView browser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initializing
        browser = (WebView) findViewById(R.id.webView1);

        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setLoadWithOverviewMode(true);
        browser.getSettings().setUseWideViewPort(true);
        browser.getSettings().setBuiltInZoomControls(true);

        // Loading
        browser.loadUrl("http://stackoverflow.com");


        browser.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);

                return true;
            }

            public void onLoadResource(WebView view, String url) {

                //here I get the Url, but its not accurate. Sometimes it works, sometiems it doesn't 
                    Toast.makeText(getApplicationContext(), browser.getUrl(),
                            Toast.LENGTH_SHORT).show(); 
            }


        });

    }

}

Upvotes: 0

Views: 12097

Answers (2)

Karan Khurana
Karan Khurana

Reputation: 585

I know its very late but,for other users if it helps it would be my pleasure. In ur WebViewClient u can use :

private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {         
            String webUrl=view.getUrl();
            return true;
        }
    }

Upvotes: 1

Pratik Sharma
Pratik Sharma

Reputation: 13405

Try to get url from this function :

EDIT:

        browser.loadUrl("http://stackoverflow.com");

        browser.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show(); 
                Log.v("TEST", url);
                if(url.equals("http://stackoverflow.com/questions")){
                    Toast.makeText(getApplicationContext(), "SKIP", Toast.LENGTH_SHORT).show();
                }
                else{
                   view.loadUrl(url); 
                }                 
                return true;
            }
        });

Reference :

shouldOverrideUrlLoading

Upvotes: 6

Related Questions