Issue
I am trying to make a webview where the app loads a HTML page from assets if there is no internet connection. I have been following this QUESTION and i have made a CheckNetwork.java
but where shall i put this code below into my MainActivity? I am a beginner at this so please explain as simple as possible.
if(CheckNetwork.isInternetAvailable(MainActivity.this))
{
// do something
}
This is my MainActivity
public class MainActivity extends ActionBarActivity {
WebView browser;
private ourViewClient mClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClass = new ourViewClient(this);
browser = (WebView) findViewById(R.id.wvwMain);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
browser.setWebViewClient(new ourViewClient(this));
try {
browser.loadUrl("http://MyWebPage");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Solution
This should do the trick for you:
if(CheckNetwork.isInternetAvailable(MainActivity.this))
browser.loadUrl("http://MyWebPage");
} else {
browser.loadUrl("file:///android_asset/your_html.html");
}
I am also including example of how you load a html from your assets.
Answered By - Boris Strandjev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.