I am developing a mobile app. On some options I am redirecting the user to different websites like http://m.moneycontrol.com/ , http://www.redbus.com/ etc.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.loadUrl("http://www.m.moneycontrol.com/");
// mainWebView.setScrollBarStyle(MainActivity.this.SCROLLBARS_INSIDE_OVERLAY);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url){
//True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
return true;
}
});
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
When I tested the app on some phones. On 2-3 phones the webview started redirecting to different sites and started opening affiliate ads to install antivirus etc.
Please Suggest me solution to stop these ads.
Change the useragent string of the WebView to that of IE or desktop Firefox. The websites will show the regular version of the site and hopefully the ads won't be there.
webview.getSettings().setUserAgentString(
"Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/12");
The general recommendation is not to open in your app's WebView any sites that you don't control. Instead of doing that, just launch the system browser:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.m.moneycontrol.com/")));
Related
I'm developing an app that should display a web page and from a link in that page open another app, let's call it otherApp. I display the web page in my app with WebView;
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().indexOf("example")>0) {
// 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;
}
});
myWebView.loadUrl("https://www.example.com/demo.html");
The demo.html file contains the link:
Start otherApp
Using the tablet's browser, clicking the link opens otherApp as expected with Chrome still running. However when using my app, otherApp opens in WebView and I need both apps to be running independently of each other. The code was taken from developer.android.com, I'm using a Nexus 7 tablet running Marshmallow.
Any ideas what I am missing?
So I've been trying to get one of my activity to display a published google sheet webpage... and I managed to get it working on my emulator (nexus 5, API 22) but when I run the app on my phone (Samsung galaxy S7) it printed out a toast message "failed to find document. it is possible the document has been deleted"
I've tried testing with and it works in both the emulator and the phone so I think the code serves it's purpose. I really don't understand...
here is a copy of my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
WebView webView = new WebView(this);
webView.loadUrl("https://docs.google.com/spreadsheets/d/e/2PACX-1vSnBA-tNsQsUKOUCs8PkdADgMP2n4TFyl8JtKFkxUanIoXbcC9xuzY89Xw9oIRCL0ane3MKpZYRFrP1/pubhtml?gid=0&single=true");
WebSettings settings = webView.getSettings();
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
}
I assume Google is redirecting your url to another url. So can you try the below code for handling redirect urls in WebView.
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
I have a webview in my android app, but when someone navigates around the site, it opens in a new window, i want it to stay inside the webview.. is there a way to do this easily? Here is the code in my activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
WebView webview;
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.co.uk");
You'll have to create a WebViewClient:
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
And then set it to your WebView like this:
webview.setWebViewClient(new myWebViewClient());
Or you can just do that, without creating a new class:
myWebView.setWebViewClient(new WebViewClient());
It works for me.
webview.setWebViewClient(new WebViewClient());
If you want to customize then you should override shouldOverrideUrlLoading (WebView view, String url). but it's deprecated in API 24. You can use public boolean shouldOverrideUrlLoading (WebView view,WebResourceRequest request). actually both of them needs to return false.
Android WebView Example
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;
}
}
There is a WebView which loads mobile-optimized URL (webpage). But when I click on a link, it does not load inside of the WebView (inside of the app), but mobile browser opens.
How to prevent this?
I tried overloading URLs via shouldOverrideUrlLoading(), but it did not help.
This is a code.
webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setPluginsEnabled(true);
if (Build.VERSION.SDK_INT > 7) {
webSettings.setPluginState(WebSettings.PluginState.ON);
}
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(url)) {
view.loadUrl(url);
return true;
}
return false;
}
#Override
public void onLoadResource(WebView view, String url) {
}
});
webView.loadUrl("http://some-url.com");
EDIT
Does GET or POST posting methods have anything with links' clicks open mobile web browser???
Return true instead of false in shouldOverrideUrlLoading.
From the documentation:
shouldOverrideUrlLoading returns True if the host application wants to
leave the current WebView and handle the url itself, otherwise return
false.