-1

i created an app for webview but in my webview I want an internet alert box that wants to popup when internet connection is not there. my javafile

`

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

    webview =(WebView)findViewById(R.id.webView);
    spinner = (ProgressBar)findViewById(R.id.progressBar1);
    webview.setWebViewClient(new CustomWebViewClient());

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
    webview.loadUrl("https://moodle.kluniversity.in/login/index.php");

}

// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView webview, String url, Bitmap favicon) {

        // only make it invisible the FIRST time the app is run
        if (ShowOrHideWebViewInitialUse.equals("show")) {
            webview.setVisibility(webview.INVISIBLE);
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {

        ShowOrHideWebViewInitialUse = "hide";
        spinner.setVisibility(View.GONE);

        view.setVisibility(webview.VISIBLE);
        super.onPageFinished(view, url);

    }
}

and thats the information those are my java file .what i want is a internet alert box enter image description here like shown in the image.please help me in this .hope i will get the answer

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • Refer to : 1) [how to check if internet connectivity is available or not](https://stackoverflow.com/a/16124915/5990846) and 2) [how to show alert dialog](https://stackoverflow.com/questions/2115758/how-do-i-display-an-alert-dialog-on-android). Apply first solution in `onCreate()`, if it's not available show dialog(solution 2); otherwise load webview. – Jay Feb 01 '19 at 13:10

1 Answers1

0

Check network connection before trying to load URL in WebView:

public boolean isOnline() {
    ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

    if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()){
        Toast.makeText(context, "No Internet connection!", Toast.LENGTH_LONG).show();
        // Your code to show dialog..
        return false;
    }
   return true; 
}

Keep in mind above method does not check if there is an active internet connection, but only if device is connected to WiFi or Data.

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126