How to open external URL in default browser and all internal links in WebView Android? -


i using webview in page , using local file assets displaying in webview in main html page external website (not local) , want open link in default browser on users device this picture disciple situation

this code in 'oncreate' method

webview v;  v=(webview) rootview.findviewbyid(r.id.webview1); v.getsettings().setjavascriptenabled(true); webviewclient vc= new webviewclient(); v.setwebviewclient(vc); v.loadurl("file:///android_asset/home.html"); 

when run application internal link working external link "www.apple.com" en in web view

i searched same question , found solution still external link opens in webview

webview webview = (webview) rootview.findviewbyid(r.id.webview1); webview.setwebviewclient(new mywebviewclient()); string url = "file:///android_asset/home.html"; webview.getsettings().setjavascriptenabled(true); webview.loadurl(url); 

and class

class mywebviewclient extends webviewclient {     @override     public boolean shouldoverrideurlloading(webview view, string url) {         if(url.contains("http")){ // cleverer , use regex             return super.shouldoverrideurlloading(view, url); // leave webview , use browser         } else {             view.loadurl(url); // stay within webview , load url             return true;         }     } } 

change

  if (url.contains("http")) { // cleverer , use regex     return super.shouldoverrideurlloading(view, url); // leave webview , use browser   } else {     view.loadurl(url); // stay within webview , load url     return true;   } 

to

 if (url.contains("http")) { // cleverer , use regex     intent intent = new intent(intent.action_view, uri.parse(url));     mcontext.startactivity(intent);     return true;  }  return false; 

note : replace mcontext activity context.


Comments

Popular posts from this blog

java - BeanIO write annotated class to fixedlength -

Using Java 8 lambdas/transformations to combine and flatten two Maps -

How to connect android app to App engine -