Android Webview - Custom HTML

How can I show my custom HTML code if there is no data connection on the phone?

Custom HTML code:

String customHtml = "<html><body>Hello, WebView</body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");

Here is my full code:

package com.webbrowser.android;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private WebView webView;

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

    webView = (WebView) findViewById(R.id.webView1);
    webView.setWebViewClient(new WebViewClient()); 
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://domain.com");
}

}

EDIT:

Is this right? It does't work on the emulator. Could you please look at it again?

EDIT2:

Added brackets.

public class MainActivity extends Activity {

    private WebView webView;

    public boolean isConnected() {
        ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        return (cm.getActiveNetworkInfo() != null && 
           cm.getActiveNetworkInfo().isConnectedOrConnecting());
    }

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

        webView = (WebView) findViewById(R.id.webView1);
        webView.setWebViewClient(new WebViewClient()); 
        webView.getSettings().setJavaScriptEnabled(true);

        if(!isConnected()) {
            String customHtml = "<html><body>Hello, WebView</body></html>";
            webView.loadData(customHtml, "text/html", "UTF-8");
        } else {
            webView.loadUrl("http://domain.com");
            }   

    }
}

Answers


First check for the connectivity using:

public boolean isConnected() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    return (cm.getActiveNetworkInfo() != null && 
       cm.getActiveNetworkInfo().isConnectedOrConnecting())
}

Then depending on the result call the appropriate method:

if(!isConnected()) {
    String customHtml = "<html><body>Hello, WebView</body></html>";
    webView.loadData(customHtml, "text/html", "UTF-8");
} else {
    webView.loadUrl("http://domain.com");
}

Since you are loading a specific html code (basic) it will load without a data connection...


Please use the webviewclients function onReceivedError. This will work regardles of connection type.

public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
        // Load custom data into webview depending on errorcode
    }

Hey u can try this code , It works very nicely

public class SudhirAwesome extends Activity {
WebView webView;
String html = "<!DOCTYPE html>\n" +
        "<html>\n" +
        "<head>\n" +
        "</head>\n" +
        "<body>\n" +
        "\n" +
        "<h1>Can you please check your Internet Connection</h1>\n" +
        "\n" +
        "</body>\n" +
        "</html>\n";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sportee);

    webView = (WebView) findViewById(R.id.webview_sportee);
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.setWebViewClient(new MyBrowser());
    if(haveNetworkConnection()){
        webView.loadUrl("http://blah.blah.com");
    }
    else{
        webView.loadData(html,"text/html", "UTF-8");

        Toast.makeText(getApplicationContext(),"Please Check your Internet Connection",Toast.LENGTH_LONG).show();
    }

    //This acts like browser only
}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        try {
            view.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

and U have to give these permissions also

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

Need Your Help

Load testing eJabberd with Tsung on EC2

amazon-ec2 ejabberd tsung

I have Tsung loaded on one EC2 instance, i also have another EC2 instance with eJabberd. I want to load test eJabberd with Tsung.All ports and security groups are configure to all inbound, and all

Hbase - copy one row from one table to another in diff env - Java

java hbase

I created HBaseConfiguration for both the environments.