Android: WebView - open specific URLs inside a WebView, the rest from the outside?

Here is my code:

public class MainActivity extends Activity {

@SuppressLint("SetJavaScriptEnabled") @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView mywebview = (WebView) findViewById(R.id.webview);
    mywebview.loadUrl("http://www.shufflemylife.com/shuffle");
    WebSettings webSettings = mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mywebview.setWebViewClient(new WebViewClient());

}

class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains("/shuffle")){
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
            }
        return true;

        }
    }
    }

Basically, I want any URL containing '/ shuffle' to load inside the WebView, and everything else that needs to be opened in an external browser. Is this doable? How can i do this?

Thanks for any help!

+5
source share
2 answers

Is this doable?

Yes.

How can i do this?

, . WebView - . , url.contains("/shuffle"), loadUrl() WebView, , true . URL-, , false.

+3

, shouldOverrideUrlLoading:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.contains("/shuffle")) {
        mWebView.loadUrl(url);
        return false;
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
        return true;
    }
}
+4

All Articles