How to open a new browser from WebViewClient?

A short question that I cannot understand, I would like to launch a new browser from my WebView after people click on the hyperlink. But how can I set this target for this link to exit WebViewClient?

Here is my code, any help is appreciated:

WebView site = (WebView)findViewById(R.id.WebView); site.setWebViewClient(new WebViewClient()); site.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); site.getSettings().setJavaScriptEnabled(true); button1.setOnClickListener(button1OnClickListener); button2.setOnClickListener(button2OnClickListener); button3.setOnClickListener(button3OnClickListener); button4.setOnClickListener(button4OnClickListener); final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); progressBar = ProgressDialog.show(FlitsersActivity.this, "Thingy1", "Load...", false, true); site.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i(TAG, "Loading..."); view.loadUrl(url); return true; } public void onPageFinished(WebView view, String url) { Log.i(TAG, "Done: " +url); if (progressBar.isShowing()) { progressBar.dismiss(); } } public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Log.e(TAG, "Error: " + description); Toast.makeText(MyActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show(); alertDialog.setTitle("Error"); alertDialog.setMessage(description); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { return; } }); alertDialog.show(); } }); site.loadUrl("http://www.etcetera.com"); } 
+4
source share
2 answers

Thanks, thanks! I think I found out when you wrote the second answer;)

Now I use the following code: it works like a charm :) Thanks a lot for the tip!

 site.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { String myAlternativeURL = "http://newURL"; if (!url.equals(myAlternativeURL)) { { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse("http://newURL")); startActivity(i); } return true; } else { Log.i(TAG, "Loading..."); view.loadUrl(url); return true; } } 
+8
source

You override the loading of all URLs, causing WebView to load them with this code:

  public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i(TAG, "Loading..."); view.loadUrl(url); return true; } 

What you need to do is create a browser URL with a URL if you want it to open in your default browser instead of your WebView. http://developer.android.com/guide/appendix/g-app-intents.html

The code would be something like this:

 Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); 

Additional Information:

 if (url.equals("http://www.etcetera.com") // load url normaly else // Load in new window via intent 
+1
source

All Articles