How to apply custom css to android Webview when URL is from internet server?
When the page is loaded, run some JavaScript, something like:
mWebView.loadUrl("javascript:document.getElementsByClassName('footer')[0].style.display = 'none';");
should do it. On Android 4.4, you can use the WebView.evaluateJavaScript API.
Detecting when the page is loaded such that this JavaScript will work is tricky. The most reliable way to do it would be to install a JavaScript Interface that calls back to Java when the onload
event is fired in your HTML. You'd want to have something like this in your Java, before your first call to WebView.loadUrl
:
mWebView.addJavascriptInterface(new Object() {
@JavascriptInterface
public void onload() {
// This is executed on a background thread, so post back to UI thread.
mWebView.post(new Runnable() {
@Override
public void run() {
mWebView.loadUrl("javascript:...");
}
});
}
}, "android");
And then in your HTML:
<body onload="window.android.ready();">
Hope this helps.
Android Webview not reading CSS from my web server
Some solutions for the issue. Try these, don't know if they will solve as you haven't provided any code or url link with question.
Did you enable javascript in webview ? If not then enable using the code below :
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
There is a known bug in the file protocol for a few android versions. If you are referencing files with url parameters a HTTP 404 (file not found) occurs and the file is not loaded.
In your case the the styles.css might not be getting loaded.
Get help here :
https://coderwall.com/p/xnbawa
http://bricolsoftconsulting.com/fixing-the-broken-honeycomb-and-ics-webview/
android webview load html from server and other files from assets
Firstly you need to have relative paths
<img src="images/someimage.png">
not
<img src="www.mysite.com/images/someimage.png">
Then you need to load the HTML code not using webBrowser, for example like here
And then you can load HTML source into WebBrowser using base URL pointing to your assets like here
How to render css file from url in to the webview in android
I have faced such type of problem, May be helpful for you,
webView=(WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
StringBuilder sb = new StringBuilder();
sb.append("<HTML><HEAD><LINK href=\"http://test.com/css/test.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>");
sb.append(attributes.toString());
sb.append("</body></HTML>");
webView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
Hopefully, it works.
related Links:
and github plugin: https://github.com/NightWhistler/HtmlSpanner
Android WebView renders blank/white, view doesn't update on css changes or HTML changes, animations are choppy
Android Webview does not open CSS/JAVASCRIPT server side php
I have fixed this issue...
First create a webviewclient class :
public class MyBabyclient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
view.LoadUrl(request.Url.ToString());
return false;
}
}
Then assign it to your webview : web_view.SetWebViewClient(new MyBabyclient());
More info can be seen here : https://learn.microsoft.com/en-us/xamarin/android/user-interface/controls/web-view
Showing HTML page with CSS attached in a WebView
The problem is the relative path href="/maindir/css/sites.css"
:
Solution 1: (change at server)
In case you have access to web-server try using full path in <link type="text/css" rel="stylesheet" href="/maindir/css/sites.css">
. So instead of /maindir/css/sites.css
you will have http://your.domain.com/maindir/css/sites.css
Solution 2: (change at client)
Instead of mWebView.loadData(data, "text/html", "utf-8");
use method loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl) the baseUrl
string will be used in order to make relative path to work.
Result will be something like:
mWebView.loadDataWithBaseURL("http://your.domain.com/", data, "text/html", "utf-8", null);
Related Topics
Set Rowspan or Colspan of a Child of a Gridlayout Programmatically
Automatic Versioning of Android Build Using Git Describe with Gradle
Recyclerview with Multiple Countdown Timers Causes Flickering
Webview with Asynctask on Android
Google Maps API V2 Class Not Found
How to Record Audio on Webpage (iOS, Android, Pc/Mac) - No Flash
How to Make Grid View Scroll Horizontally Not Vertically in Android
Android Emulator Failed to Allocate Memory 8
Find the Physical Address of Exception Vector Table from Kernel Module
Android- Phonegap/Webview Ignores Viewport Meta Tags
Onitemclicklistener Was Not Work with the Checkbox
Onpagefinished() Never Called (Webview)!
Running Google Map Application on Android Emulator
Sqlitedatabase Close() Function Causing Nullpointerexception When Multiple Threads
Clearing Notification After a Few Seconds
I Want Show Notification at 8:00 Am Everyday
How to Show a Preview of a Recyclerview's Contents in The Android Studio Editor