Trying to pass the URL from webview to browser
-
06-04-2021 - |
Question
I am using a webview of height 60dp and i am passing a local html file to it, by default when i click a link in the webview it has to open the browser . But strangely its opens the links with in the webview, i also tried the webview client and trying to pass the response url to default browser through intent, but in vain..
my code snippet:
WebViewClient yourWebClient = new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
System.out.println("Inside WebViewClient the URL is....."+url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
}
};
WebView ad = (WebView) findViewById(R.id.webview1);
ad.getSettings().setJavaScriptEnabled(true);
ad.loadUrl(feed.getItem(position).getLink());
ad.getSettings().setLoadWithOverviewMode(true);
ad.getSettings().setUseWideViewPort(true);
ad.setInitialScale(100);
ad.setWebViewClient(yourWebClient);
ad.loadUrl("file:///android_asset/advertisement.htm");
Solution
Stop the current webview loading. As ad is the object of WebView, Try doing it this way:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
System.out.println("Inside WebViewClient the URL is....."+url);
ad.stopLoading();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Also add this while adding settings for your WebView:
ad.getSettings().setSupportMultipleWindows(false);
Edit:
Try associating a chrome client with your webview and then check:
ad.setWebChromeClient(new MyWebChromeClient());
Hope this works for you.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow