Okay i have a webview which loads webpage when it is online (for example google.com) but when it is offline it it says The webpage could not be loaded.. i want to show the last webpage when phone was connected a the internet.
i am new, so can you just make me a java class so that i can just copy/paste.
My Source code
public class tab1 extends Fragment {
WebView webView ;
ProgressBar progressBar;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.tab1,container,false);
webView = (WebView) view.findViewById(R.id.webview2);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line.
// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
// String appCachePath = this.getCacheDir().getAbsolutePath();
// Log.e(TAG, "appCachePath = " + appCachePath);
// webView.getSettings().setAppCachePath(appCachePath);
progressBar = (ProgressBar)view.findViewById(R.id.progressBar2);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.loadUrl("http://google.com");
return view;
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
}