We are trying to allow our users to download / display PDF files in a web application (WebView)
The HTML is a simple
<iframe src="path/to/pdf" />
In a standard android browser everything works - when clicking the link, the browser is allowing either display the PDF, or download... (content-type is application/pdf)
When using the webview we do not get the same results - a blank page is showing, (maybe a new tab is not opening correctly?!)
Note - We cannot use Google Viewer - the PDF is in a user restricted area (a session is required) - and so we cannot provide a public link to the PDF, which is required for google viewer to work.
How can we make the webview work just like the regular browser?
HTML is simple but it contains many css, javascript/jquery code dependencies. Which browser renders accordingly and javascript can run on browsers. As browsers are preconfigured to run javascript and render html css.
Android webview is not preconfigured to run javascript, css. It can render html only. So it looks weird in webview as it renders without css and does not run javascript
How to configure webview to run css and javascript, allow go forward in webview and go backword like browser?
Here are full properties listed. Android developers
Then your webview can let users download the file present there..
WebView mWebView = findViewById(R.id.myWebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new MyWebChromeClient((Activity)mContext));
mWebView.addJavascriptInterface(new testClass(), "jsinterface");
mWebView.loadUrl("UrlToLoad");
You need to append http://docs.google.com/gview?embedded=true&url= to open PDF file in Webview
String docInitialUrl = "http://docs.google.com/gview?embedded=true&url="+docUrl;
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.setInitialScale(100);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(docInitialUrl);
Related
How can I use AngularJS & H5 for building an Android native/hybrid app without using PhoneGap or Cordova, just simple webview inside android layout file.
Please provide some reference :)
Any AngularJS web app runs fine in a WebView.
To run the website in a WebView all you need to do is this.
final WebView containerWbVw = findViewById(R.id.fragment_wbVw_container_id);
WebSettings webSettings = containerWbVw.getSettings();
webSettings.setJavaScriptEnabled(true);
containerWbVw.setWebChromeClient(new WebChromeClient());
and then load the URL in the WebView using
containerWbVw.loadUrl(WEBAPP_URL);
The WEBAPP_URL can be an HTML page in your assets folder or hosted on your server
//assets folder file
WEBAPP_URL = "file:///android_asset/main.html";
//server link
WEBAPP_URL="https://mysite.mycompany.com/myapp";
In addition to this, you would need to interact with the native android app to send and receive data. For example, a use case would be to get a scanned barcode using the device's camera. For this, you would need a Javasrcript Bridge.
I am trying to load a HTML page which will have an option to choose the .dae file(3d model) using file explorer and it should be loaded in webview. This is working fine in chrome browser but not in webview.
I have enabled the below settings.
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDomStorageEnabled(true);
I am able to choose the .dae file and after that nothing happens. Whereas it's loading the 3D model in chrome browser of the device.
Can someone help me on this? Do I need to enable any other settings? Thanks in advance.
Mobile webview is not the Movile browser, they have different render ability.
WebView is render ability cannot match the browser.
To sove this, use XWalkView of crosswalk to replace webview.
How to use crosswalk to render 3D models refer to my answer here.
I am creating an application and I am trying to load a HTML page in a web view.
I am using the code
WebView webView = (WebView)findViewById(R.id.webview);
webView.loadUrl("file:///android_asset/index.html");
but continue to receive an error that the web page at file:///android_asset/index.html might be temporarily down or it may have moved permanently to a new web address.
Any help would be great
from assets folder
webview.loadUrl("file:///android_asset/file.html");
from specific folder
webview.loadUrl("file:///data/data/com.example.example/files/file.html");
My application has a list of pdf inside listview. The pdf is viewed inside the application using a webview that open a Google Docs sheet. It works fine for the first opened PDF, but
when going back and opening another PDF I got the error "whoops! There was a problem previewing this document". Is there some kind of limitation on opening PDF using google sheet. If so, how can I get full rights to open any PDF I want. can someone help please. Here is my code.
//---you need this to prevent the webview from
// launching another browser when a url
// redirection occurs---
wv.setWebViewClient(new Callback());
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl("https://docs.google.com/gview?embedded=true&url=" + SharepreferenceController.getPrefSavePdfInfo());
Try increasing the Version/Compatibility of the PDF. This worked for me.
Edit: I believe it is more of a size issue. When I changed the Version (from Acrobat 9.0 to 10.0) I had also optimized the PDF making it a smaller file. My larger files of the same Version still do not work.
I was having the same issue. What solved it for me was clearing the WebView cache before trying to load another PDF.
WebView webView=(WebView)findViewById(R.id.webView);
webView.clearCache(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://docs.google.com/viewer?url=" + url);
I am developing an android app. In that I have used webview to open the html page.
But some of the features on my HTML page would work only if I access the page through server.
How can I host the HTML page locally on my Android device?
For better Idea:
Example: On PC, I have hosted the html page locally using Apache Tomcat Server.
So to load this HTML page, I have two options
1. Directly double clicking the HTML and opening in browser.
2. Using URL: http://localhost:8080/MyWebApp
The first option is basic and some of features are not supported.
In second approach, all the features work as expected.
Is there any app/sdk/ open source which I can use to locally host the HTML page?``
try this code
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
//webView.loadUrl("file:///android_asset/folderName/index.html");
webView.loadUrl("http://10.0.2.2:8080/MyWebApp");
Put your file to assets folder and write this
webView.loadUrl("file:///android_asset/help.html");