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;
}
});
Related
I have a web page with a basic form (like a newsletter subscription).
The user sends its information and gets a success page with a link to the form submission page. If he doesn't click on the link the page will be automatically redirected after 5 seconds.
<meta http-equiv="refresh" content="5;url=www.site.com" />
So far so good! It works as expected in both desktop and mobile browsers.
Then I created an android app (min API SDK 21) with a web view to load the site.
The problem is that the auto refresh isn't working inside the web view...
Am I missing something? I'm trying to avoid the javascript hack...
Thanks for your time!
EDIT
I notice that when I click on the new form submission page link it opens the default browser instead of rendering it on the web view!
After searching for the problem, I came to a solution by resetting the URL on URL loading.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return true;
}
});
mWebView.loadUrl(url);
}
It's working, but I think it can be improved.
My initial problem was that I wasn't setting a WebViewClient.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.loadUrl(url);
}
Thus, I think I just need to add a WebViewClient and it'll work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
Note, and if we want javascript to work we need to enable it
mWebView.getSettings().setJavaScriptEnabled(true);
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?
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/")));
I am trying to load a wiki web page into a native Android WebView with WebView.loadUrl(String url). It works and displays all of the web content, except for the images. However, I have not found a reason for this. Does anyone know why image resources would not load automatically? Is there a method I need to overwrite or a parameter that needs to be set? I can't find anything in the documentation - it looks like the default settings should allow for automatic image loading. Any information would be appreciated. Thanks!
mWebView = (WebView)findViewById(R.id.webView);
setWebViewClient(mWebView);
setWebSettings(mWebView);
mWebView.loadUrl("http://my_wiki_url_goes_here/");
private void setWebViewClient(WebView mWebView) {
WebViewClient client = new WikiWebViewClient();
mWebView.setWebViewClient(client);
}
private void setWebSettings(WebView mWebView) {
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
}
public class WikiWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
Printing the url onLoadResource(), I see that the image urls generally start with "data:image/png;base64,". I don't think that should makes a difference or not - it works in the web browser.
Huh, looks like simply enabling DOM storage did the trick.
settings.setDomStorageEnabled(true);
If I don't receive a better explanation or solution, I will accept this answer once I am able to
In my android app the user can like my facebook page. Therefore I use a webview that loads a webpage that contains a facebook like box. The page is loaded well into the webview and then the following happens:
WebView loads my webpage with a facebook like box
By clicking on the Like button the user is redirected to the facebook login page
After login the user is redirect again back to my custom like page
But when clicking the like button the user is again redirected to the facebook login page
So I would expect, that the like is possible after logging in. It seems somehow as if the webview does not remember the login. Therefore, what do I have to do to repair this.
The following screenshot sequence shows what's happening:
I want to avoid using the facebook sdk for android! Especially this procedure works very good in my iphone version of the app.
Here is some code that is used to implement my desired functionality:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView fbWebView = (WebView) findViewById( R.id.facebookWebView );
fbWebView.getSettings().setJavaScriptEnabled(true);
fbWebView.setWebViewClient( new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
Log.d("call url", url );
if ( url.contains("login.php?skip_api_login") ) {
fbWebView.loadUrl("http://www.example.de/iphone_facebook_like_page.html");
return null;
}
if( url.contains("user_successfully_liked") )
fbWebView.loadUrl("http://www.example-success.de");
return null;
}
});
fbWebView.loadUrl("http://www.example.de/iphone_facebook_like_page.html");
}
EDIT: I also tried the following to accept cookies but none of this worked
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
CookieManager.setAcceptFileSchemeCookies(true);
CookieManager.getInstance().setAcceptCookie(true);
//rest is the same...
Overrding didn't work either
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
You have that error, because WebView isn't remembering cookies. Read that question on SO : WebView and Cookies on Android
Not sure if you've gotten an answer to your problem, but I tried a combination of these instructions and the code below, and that worked for me:
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
...
WebView webView = new WebView();
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
settings.setDomStorageEnabled(true);
settings.setGeolocationEnabled(true); // this last one is probably optional
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(<your url here>);
...
}
You will then need to call the corresponding methods for the CookieSyncManager singleton class as outlined in the link above. Note that the sync class is deprecated in API Level 21: see details on CookieSyncManager class