Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap not working in Android WebView

HTML file(test.html) is

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <link href="bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="bootstrap-responsive.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>Hello World!!!</h1>
 <a class="btn btn-primary btn-large">
    Learn more
</a>
</body>
</html>

HTML and bootstrap css files are placed in assets folder. If I open the HTML file in IE/Firefox, it is displayed correctly. But Android WebView does not render this properly

Code for WebView is

WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
String url = "file:///android_asset/test.html";
webView.loadUrl(url);

IE renders it like this

IE Renders it like this

WebView renders it like this

WebView Renders it like this

What is wrong?

like image 963
Vivek Avatar asked Apr 22 '13 11:04

Vivek


2 Answers

Add this line

webView.getSettings().setDomStorageEnabled(true);
like image 191
Rohit Chemburkar Avatar answered Oct 14 '22 00:10

Rohit Chemburkar


Tested on emulator - Android version 4.2 with Bootstrap v2.3.2

Files in my app's assets folder:

 bootstrap-responsive.css
 bootstrap.css
 test.html

No need to add "android.permission.INTERNET" in Android manifest since css files are available locally within the app's asset folder.

Here's a screenshot of how it is rendered. There is no reason why you could not see it unless there are issues with the CSS files (which is again unlikely since you can see the results on browser).

enter image description here

like image 21
random Avatar answered Oct 13 '22 23:10

random