Does any url open in webview?

Asked

Viewed 185 times

1

I have my job:

        webView.setWebViewClient(new WebViewClient(){
            @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("http://www.hotelcolonialdosnobres.com/")) {
                view.loadUrl(url);
                return true;
            }return false:
}

However I wanted only my website to open in the webview the rest to open in the normal browser.

1 answer

1


try so o:

public class WebViewClientImpl extends WebViewClient {
    private Activity activity = null;
    public WebViewClientImpl(Activity activity) {
       this.activity = activity;
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if(url.indexOf("hotelcolonialdosnobres.com") > -1 )
            return false;
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        return true;
    }
}

Thus, if the url contains your site, shouldOverrideUrlLoading returns false and returns to the webview call.

dai Voce calls him so:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        WebViewClientImpl webViewClient = new WebViewClientImpl(this);
        webView.setWebViewClient(webViewClient);
        webView.loadUrl("http://www.hotelcolonialdosnobres.com/");
    }
}

Note that to instantiate Webviewclientimpl is passed the same Activity "new Webviewclientimpl(this);" as a parameter.

to learn more about this, visit Jakob Jenkov tutorial which is where I got the reference.

hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.