Android WebView won't load local files - android

I'm having a problem with Android WebView for my websites. When I open my site on any browser, the CSS style sheet, JavaScript and Images load. But when I open for example through Instagram (which uses WebView), I just have the html, naked.
I precise that all my links are in relative (I have a tag which indicates from where to look for the files).
Here, my base is <base href="http://anthony-e-b.rf.gd/" target="_top">
and my CSS, my images, and my JS refer to this as:
<link rel="stylesheet" href="assets/main.css"> (for the CSS).
Same for my pictures : href="assets/pics/mon_image.jpg".
I do not understand, and although I search the Internet I do not see any solution ...
thank you in advance for your help

Try:
WebView myWebView = findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Hope it will help!

Related

Android WebView with css and javascript

Many of the answers I've been seeing here are 6+ years old so I was wondering if I could get a more recent answer as they haven't been working for me. I have an html file file.html which contains a
<LINK href="stylesheet.css" type="text/css" rel="stylesheet">
for the CSS and the following for the javascript:
<script src="myscript.js"></script>
file.html, stylesheet.css and myscript.js are all stored in app/assets/Content/
So I am trying to run it with:
WebView webView = (WebView) findViewById(R.id.event_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file:///assets/Content/file.html");
However, the WebView is showing the error:
Webpage not available: The webpage at
file:///assets/Content/file.html could not be loaded because
net::ERR_FILE_NOT_FOUND
I have also considered using loadDataWithBaseURL but this requires the data field to contain all of the html which is too big of a file to hardcode.
Does anybody know how I could use html+css+javascript for a WebView?
Your URI for asset folder is incorrect. Change asset to android_asset. As follows:
webView.loadUrl("file:///android_asset/file.html");

Stripe.js credit card input form invisible in Webview

I was trying to test if it is possible to integrate stripe js in a website and load that site in an android Webview to proceed for payment. I was using this link https://github.com/stripe/elements-examples to load that in Webview like this:
WebView browser = (WebView) findViewById(R.id.webview);
browser.loadUrl("https://stripe.github.io/elements-examples/");
Everything is loading in the Webview except the credit card field is not there. Either it is not visible or it can not be editable. Why is that?
Try to add below code to enable Javascript in your Webview:
WebSettings webSettings = browser.getSettings();
webSettings.setJavaScriptEnabled(true);

Webview Android does'nt load whole content from a large html file

I have a very large html file(around 10k lines of only text). When I use Webview to load this html content, it shows only around few hundreds and remaining space is blank(white space). Can someone help me with this issue? Thank You.
Following is my code:
WebView mWebView;
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/test.html");//test.html in assets folder
I also try with webView.setLayerType(WebView.LAYER_TYPE_HARDWARE, null) or webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null) but it does'nt help.
I just found solution for my issue with Webview. The reason Webview can't display whole content of html file because the html is not well constructed, some html tag like <abc#.com> will not be read by Webview. So I convert content to correct html form then it works.

Android WebView don't show html document

I have a html document, I've already tested on the html editor online website (http://htmledit.squarefree.com), it worked fine. However, WebView on android don't show this document, it only show text plain. I have very little experience with html, can anyone help me?
Make sure that your AndroidManifest.xml file includes
<uses-permission android:name="android.permission.INTERNET"/>
just after the
<uses-sdk />
tag. Without this, your app won't be able to access the Internet and therefore webview won't load anything.
HTH
Try -
WebView myWebView = (WebView) findViewById(R.id.webview1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
This will enable javascript

How to add local css file to a page in WebView?

So, I have a WebView showing page from url:
WebView webView = (WebView) findViewById(R.id.showContentWebView);
String url = "http://edition.cnn.com/2014/02/05/sport/shaun-white-sochi-slopestyle/index.html";
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
And I also have a file "style.css" in my assets folder. How can I display the page in WebView with style.css connected?
I think this is possible only if you somehow process the HMTL content, adding a link to your css file. In this case, please refer to this question.

Categories

Resources