views:

54

answers:

2

Hi all,

Short question that i cannot get my finger on, i'd like to launch a new browser from my WebView after people click a hyperlink. But how can i set that target of that link to escape the WebViewClient??

Here's my code, any help is greatly 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");

}
+1  A: 

You are overriding the loading of all URLs by forcing the 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 intent with the URL if you want it to open in the 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);

More info:

if (url.equals("http://www.etcetera.com")
   // load url normaly
else
   // Load in new window via intent
mbaird
Thank you for relying, except that if i implement this code it opens a new browser window on starting the webview. What i would like to do is: 1. Start Webview, load url. 2. After the user clicks on a hyperlink in the already loaded page, that click starts the browser with the new hyperlink in it. The same idea as what you get when implementing target="_blank" in HTML.Any idea? Thanks in advance!
Martijn
Well you obviously need to call view.loadURL(url) on the first load. See my updated answer, I wrote your if statement for you.
mbaird
Er, see answer above, made a slight error in responding. Anyway it works, thanks again for pointing me in the right direction!
Martijn
A: 

Exactly, thanks! I think i found out around the time you wrote the second response ;)

Right now i am using the code below: it works like a charm :) Thank you very much for responding!

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