Featured post

Marshmallow Features Point by Point

Android Runtime (“ART”) Improved application performance and lower memory overhead for faster  multi-tasking. Battery Doze...

Monday 15 February 2016

Dynamic html and javascript from assets in a WebView | Add external css file on html page in Android

I am trying to display HTML content in a web view. The UI is simple jQuery Mobile. The HTML displays, but the CSS and JavaScript from the assets folder.
Css and js file will resides in assets folder.

Note : Put you .css and .js file in assets folder as well

here is the implementation code

OUTPUT :



WebView  wvSpecification= (WebView)rootview.findViewById(R.id.wvSpecification);

       StringBuilder sb = new StringBuilder();
                     sb.append("<HTML xmlns='http://www.w3.org/1999/xhtml'>" +           
                                  "<HEAD>"+
                                  "<meta name='viewport' content='width=device-width, initial-scale=1'>"+
                                  "<link rel='stylesheet' href=cssfilename.css'/>"+
                                  "<link rel='stylesheet' href= cssfilename.css'/>"+
                                  "<script src= jsfilename.js' type='text/javascript'></script>"+
                                  "</HEAD>"); 

                     sb.append("<body>"+
                                  "<h1>Mobile</h1>"+
                                  "<table class='responsive'>"+
                                  "<tr>"+
                                  "<th>Company Name</th>"+
                                  "<th>Model Name</th>"+
                                  "<th>Version</th>"+
                                  "</tr>"+
                                  "<tr>"+
                                  "<td>Google</td>"+
                                  "<td>Nexus 5</td>"+
                                  "<td>4.4</td>"+
                                  "</tr>"+
                                  "<tr>"+
                                  "<td>HTC</td>"+
                                  "<td>Desire X</td>"+
                                  "<td>4.1</td>"+
                                  "</tr>"+
                                   "<tr>"+
                                  "<td>Samsung</td>"+
                                  "<td>Galaxy s4</td>"+
                                  "<td>4.3</td>"+
                                  "</tr>"+
                                  "</table>"+
                                  "</body>");
                                  sb.append("</HTML>");


                                 wvSpecification.loadDataWithBaseURL("file:///android_asset/",sb.toString(),"text/html","UTF-8",null);

No comments:

Post a Comment