Skip to content Skip to sidebar Skip to footer

How To Create/update/load A Html File At Runtime

I need to create and open an HTML file at runtime.

Solution 1:

If you want to run html file runtime then try using this code.

 webview_data.loadDataWithBaseURL("file:///android_asset/", htmlString,"text/html", "UTF-8",null);

And make sure if you want to use any css or javascript file then put it into android assest folder..

Solution 2:

There are indeed better ways of generating HTML pages in Android than manual string concatenation. There are a few slick Java libraries that will do it for you (recall that you can use regular Java libraries within your Android project pretty easily).

Solution 3:

How to create html file.

  1. Open file
  2. Write text characters comprising the HTML
  3. Close file

If you need more details, the Oracle Java Tutorial has lessons on basic Java constructs, and on how to do simple file I/O.


There are other more "elegant" ways of doing this kind of thing in different contexts (e.g. in a web service) but if this the sum total of what you need to do, then ... keep it simple.


... is there any other way to create HTML file like creating XML file using XmlSerializer.

Not HTML. You could create XHTML that way through.

Having said that, constructing HTML or XHTML that way sounds like a poor idea. The resulting code would be neither readable or efficient ... compared with string concatenation or template-based approaches.

(Try it and see ...)

Solution 4:

  1. Create MyHtml file

    MyHtml.html

    <html><body><formname="Home"method="POST"action='' ><inputid='Name'type='hidden'value="MyName"name='Name'/><inputid='FirstName'type='hidden'value="MyFName"name='FirstName'/>          
           .
           .
           .
         </form><scriptlanguage='javascript'>//java script</script></body></html>
  2. stored in Assets folder as MyHtml.html

  3. Read and update this file in my app like below

    InputStream is = getAssets().open("MyHtml.html");
        int size = is.available();
    
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
    
        String str = new String(buffer);
        str = str.replace("MyName", "James Bala");
        str = str.replace("MyFName", "James");
        .
        .
        .
    
  4. then i loaded my html file in webView like below

    myWebView.loadData(str, "text/html; charset=utf-8", "UTF-8");
    

As Stephen's advice finally I ended with this solutions. Thanks @Stephen

Post a Comment for "How To Create/update/load A Html File At Runtime"