Trying to pass the URL from webview to browser - android

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");

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.

Related

open a new browser from webview

I created an android app with webview.
Within the web page I have amazon banners.
If a user clicks on that banner I would like it to be open in the native android browser and not in webview.
I've tried several methods after searching and trying with no success.
When the banner is clicked - the app still shows the amazon site in webview mode.
Can anyone help me fix this issue ?
Your help is greatly appreciated...
Below is the code I use :
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.contains("amazon-adsystem.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
view.loadUrl(url);
loadAd();
return true;
}
});
webView.loadUrl("http://starter_url");
loadAd();
The function shouldOverrideUrlLoading() should:
Returns True if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
So you have to change your code to something like:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.contains("amazon-adsystem.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
}
return false;
}
});

Android - Bug in Webview? Some URL's prompt webview to start external browser

I have a "NewsActivity" with a webview. When I start it from my main activity, links are opened in the webview correctly but, for some reason, some url's cause the webview to open but then to immediately launch the external browser. Checking the debug console I have not found any exception or other message thrown by webview as not being able to handle the url.
Please note that I am not talking about a link clicked after the webview has loaded the url/page.
I have also tried to activate javascript in the webview but to no avail.
Also, this happens for just some urls from the same domain (specifically a news website; also, I have no block url or override in place).
Here is one of the urls that fail to open in the webview: url_not_loaded
Here is the code that calls the "NewsActivity"
Intent intent = new Intent(this, NewsActivity.class);
intent.putExtra(MainActivity.EXTRA_MESSAGE, url);
startActivity(intent);
And here is the code in "NewsActivity"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
mWebview = (WebView) findViewById(R.id.newsv);
Log.d(MainActivity.LOG_TAG, "URL: " + url);
mWebview.loadUrl(url);
}
If someone has a clue as to what may be happening or can suggest any idea, I'll be grateful.
Thanks!
Your URL's might have redirects. I encountered a similar problem.
Add this to your activity with the webview.
webView.setWebViewClient(new Callback());
Add this outside of onCreate.
private class Callback extends WebViewClient{ //Helps to open in webview instead of browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
It's not a bug.
You need to use WebViewClient.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
System.out.println("hello");
return false;
}
});
//Toast.makeText(this, "", Toast.LENGTH_SHORT);
mWebView.loadUrl(url);
You can set a newWebViewClient like this:
webView.setWebViewClient(new WebViewClient());
But to be more precisely you need to override the shouldOverrideUrlLoading method like this:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}});

why my webview is opend in a own browser

I have a android code like this:
WebView myWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://test.com");
the problem is, when I start the app, it is opend in a new browser intend and not in my webView of the app.
how to avoid this?
Add a WebViewClient. This will prevent the default browser to open the URL (or get a selection dialog when there is no default)
myWebView.setWebViewClient(new WebViewClient());
More information can be found on Android website: http://developer.android.com/guide/webapps/webview.html
With the WebViewClient you can do more things, like preventing to load an URL, or change the URL.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

surfing all website and all links opens in same webview

when I create webview application to view any website on same webview not browser it works fine but when I click on any link in that website it opens in browser ? how to load all internal links/pages/sections when user click on them at same webview ?
Java version:
You only need to set a WebViewClient for your WebView and then all links will be opened it the same WebView:
webView.setWebViewClient(new WebViewClient());
private class myCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//here load the url in your webview
webview.loadUrl(url);
return true;
}
And this for set in your webview
webView.setWebViewClient(new myCustomWebViewClient());
Kotlin version.
webView.webViewClient = WebViewClient()
You should add a webViewClient of your own.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
in the webViewClient you can override the shouldOverrideUrlLoading() method to be like:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
I hope you find it helpful.

open another webview on clicking on ad image

I'm developing an app in which i have to display an ad,have set it's position and ad is displying but when we click on ad image this open in same webview.But i wanna when we click another webbview should open for display.Can any please help me out.Thanks
Code:
WebView ad = (WebView) findViewById(R.id.ad);
ad.loadUrl(url);
ad.setWebViewClient(new MyWebViewClient());
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Try to switch second and third line, I think the problem is you are loading url prior to setting your webviewclient:
WebView ad = (WebView) findViewById(R.id.ad);
ad.setWebViewClient(new MyWebViewClient());
ad.loadUrl(url);
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
getApplicationContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
Finally done this using.......following code
private class MyClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
I recognize that, when you comment out your ad.setWebViewClient(new MyWebViewClient());, the banner ad in the webview does start a new view.
I use only wrap the img tag with an anchor tag, that totally works in my development with Android 4.4

Categories

Resources