0

There are more questions like this but no one solved my problem:

I am trying to load a string into a webView. All works fine always when I have internet connection. When there is no internet connection, the content is not displayed. I do this:

WebView wb = (WebView) findViewById(R.id.webView1);
wb.loadData("Lorem ipsum dolor sit amet",  "text/html", "utf-8");

These are the permissions I request in Manifest.xml file:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permisson.ACCESS_WIFI_STATE"/>

As I said, it works fine with internet connection, and does not load the content when there is no internet connection. What I need to do to get the webView work without internet connection. I am targeting Android 3.2 with a 2.1 as minimal version

1 Answers1

1

Edit:

You could create your html or txt file in your assets directory and load that file:

webView.loadUrl("file:///android_asset/filename.html");

Source

Try loadDataWithBaseURL:

String html = "<html><body>Hello, World!</body></html>";
String mime = "text/html";
String encoding = "utf-8";

WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);

Code from: https://stackoverflow.com/a/4543485/480415

Community
  • 1
  • 1
RyanG
  • 4,393
  • 2
  • 39
  • 64