Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To show a static html page in android

I am trying to show an html file in my assets folder but in web view i am seeing white blank page. I got similar example from stackflow only.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final String mimeType="text/html";
    final String encoding="UTF-8";
    String htmlString="<html><body>";
    Document doc;
    WebView wv= new WebView(this);
    Elements link = null;


    setContentView(wv);
    try{
        InputStream in=getAssets().open("myweb.html");
        byte[] buffer= new byte[in.available()];
        in.read(buffer);
        in.close();
        wv.loadData(new String(buffer), mimeType, encoding);
    }
    catch(IOException e)
    {
        Log.d("MyWebView", e.toString());
    }
}
like image 420
PiyushMishra Avatar asked May 02 '11 10:05

PiyushMishra


People also ask

How do I view Web pages on Android?

String url = "http://www.example.com"; Intent i = new Intent(Intent. ACTION_VIEW); i. setData(Uri. parse(url)); startActivity(i);

Can I use HTML code in Android Studio?

Coding? Yes, that's right—coding on your Android device is not only possible, but also popular. The top HTML editors in the Google Play Store have been downloaded millions of times, proving both professionals and enthusiasts increasingly view the operating system as a viable productivity platform.


3 Answers

you can load the content of the web view using

// add a webview with id @+id/the_webwiev to your main.xml layout file
WebView wv = (WebView)findViewById(R.id.the_webview);
wv.loadUrl("file:///android_asset/myweb.html");
like image 118
P.Melch Avatar answered Oct 18 '22 17:10

P.Melch


Uhm, did you try following the WebView example from the official webpage? It's really simple.

http://developer.android.com/resources/tutorials/views/hello-webview.html

I followed that and had no trouble implementing a WebView. Your code looks overly complicated for something that is quite simple.

If your file is called pmi_help.html (and located in the /assets/ folder), you load it using:

    mWebView.loadUrl("file:///android_asset/pmi_help.html");
like image 35
Codemonkey Avatar answered Oct 18 '22 17:10

Codemonkey


Put your html page in asset > www, then load:

mWebView.loadUrl("file:///android_asset/index1.html");
like image 32
Piyush Avatar answered Oct 18 '22 16:10

Piyush