0

I have a page on my Android app which will display the contents of an html file using the code below -

 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.intro);

      WebView wv = (WebView) findViewById(R.id.WebView01);
      try {
          InputStream fin;
          fin = getAssets().open("Preface.html");
          byte[] buffer = new byte[fin.available()];
          fin.read(buffer);
          fin.close();
          wv.loadData(new String(buffer), "text/html", "UTF-8");
       } catch (IOException e) {
          e.printStackTrace();
       }
 }

The code runs ok but the contents don't show in the webview, what am I doing wrong?

4 Answers 4

1

use

wv.loadUrl("file:///android_asset/Preface.html");

be sure your file Preface.html is inside your android assets/ folder

or if your html file contains javascript code enable javascript support with

WebView wv = (WebView) findViewById(R.id.WebView01);
      try {
          InputStream fin;
          fin = getAssets().open("Preface.html");
          byte[] buffer = new byte[fin.available()];
          fin.read(buffer);
          fin.close();
          wv.loadData(new String(buffer), "text/html", "UTF-8");
    WebSettings webSettings = wv.getSettings();
    wv.setJavaScriptEnabled(true);
       } catch (IOException e) {
          e.printStackTrace();
       }

if this don't solve your problem paste your html code.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately the wv.loadUrl.. did not work and when I tried the code above I get an error saying that setJaveScriptEnabled is undefined. Strangely when I load the webview dynamically using the code WebView webview = new WebView(this); setContentView(webview); the html loads perfectly, it just wont load into the component.
0

First , display the content of the file you download in the LogCat, and see if the download is okey or not

1 Comment

The download is okay because if I load the html file dynamically by using the code - WebView wv = new WebView(this); setContentView(wv); The file loads without a problem.
0

Sometimes what happens is,your screen size becomes too small for the content to display on the screen. see to it that you are using a larger display resolution for your screen.

1 Comment

for that , he can use a scrollView, and then a webView inside it ,
0

My following code loads html-content from a Url and displays it in the webview:

MainActivity.java

String htmlContent = getHtmlContent();
            if (htmlContent != null) {
                webView.getSettings().setBuiltInZoomControls(true);
                webView.loadData(htmlContent, fileType.endsWith("rfc822") ? "rfc822" : "text/html", "UTF-8");
            }

getHtmlContent Method

DownloadTask task = new DownloadTask();

    try {
        String result = String.valueOf(task.execute(currentFileUrl).get());
        if (task.getStatus() == AsyncTask.Status.RUNNING) {
            loadingProgressBar.setVisibility(View.GONE);
        }
        return result;
    } catch (ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }

    return null;

and the DownloadTask.java class

public class DownloadTask extends AsyncTask<String, Void, StringBuilder> {


@Override
protected StringBuilder doInBackground(String... urls) {

    StringBuilder result = new StringBuilder();
    URL url;
    HttpURLConnection connection;

    try {
        url = new URL(urls[0]);
        connection = (HttpURLConnection) url.openConnection();
        InputStream in = connection.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);
        int data = reader.read();

        while (data != -1) {
            char current = (char) data;
            result.append(current);
            data = reader.read();
        }

        return result;

    } catch (IOException e) {
        e.printStackTrace();
    }



    return null;
}

}

One problem that I had is the WebView wasn't desplaying the content, I solved it by setting WebView webview = findViewById(R.id.webView); and removing webview = new WebView(this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.