Any url open in webview?

1

I have my role:

        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:
}

But I wanted only my site to open in the webview, the rest would open in the normal browser.

    
asked by anonymous 04.11.2016 / 00:02

1 answer

1

Try this way:

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;
    }
}

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

So you call him like this:

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 the WebViewClientImpl is passed to the same " new WebViewClientImpl (this); " activity as parameter.

To learn more about this, visit Jakob Jenkov tutorial that was where I took the reference.

I hope it helps!

    
04.11.2016 / 00:23