android - Load CSS file from assets to a WebView with custom HTML and predefined Base URL -
i need load html construct @ runtime webview , apply css file placed in assets directory. , already have base url need provide webview's loaddatawithbaseurl
function.
code snippet (not applying css file):
stringbuffer buff = new stringbuffer(); buff.append("<head>"); buff.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/my_css_file.css\"/>"); buff.append("</head>"); buff.append("<div class=\"announcement-header\">"); buff.append("header"); buff.append("</div>"); buff.append("<div class=\"announcement-separator\"></div>"); buff.append("<div class=\"announcement\">"); buff.append("content"); buff.append("</div>") webview.loaddatawithbaseurl(my_base_url, buff.tostring(), "text/html", http.utf_8, null);
i've looked @ these 2 similar issues issue 1 & issue 2, case different cant give file:///android_asset/
base url loaddatawithbaseurl
function.
any ideas how can apply css file in case ?
if want intercept of requests webview
can overriding shouldinterceptrequest()
in webviewclient
this:
public webresourceresponse shouldinterceptrequest (final webview view, string url) { if (urlutil.isfileurl(url) && url.contains(".css")) { return getcsswebresourceresponsefromasset(); } else { return super.shouldinterceptrequest(view, url); } }
there excellent detailed answer here https://stackoverflow.com/a/8274881/1112882
however can not access local file if base url not local because of security reasons. give relative path css , intercept request.
i suggest using pattern differentiate b/w actual relative , custom relative paths. e.g. use android_asset/my_css_file.css
instead of my_css_file.css
, modify shouldinterceptrequest()
accordingly intercept requests starting android_asset
.
Comments
Post a Comment