Click here to Skip to main content
15,915,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made an application in which i have used web view. but when there is no internet connection the application shows the url not found message or web page not found error message. what i want is that if there is no internet connection in the device then a toast message("Please check your internet connection") is displayed instead of loading URL. Please help in resolving this problem.
My code is below:-
Java
package com.iapits.jpsc;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.servesh.jpsc.R;

/**
 * Created by Servesh on 10-01-2016.
 */
public class WebViewMethod extends Activity {

    private WebView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        view = (WebView) findViewById(R.id.webView1);
        view.setWebViewClient(new MyWebViewClient());
        String url = "http://android-mocktest-portal.indianstudyspot.in/issjpscquiz";

        view.getSettings().setJavaScriptEnabled(true);
       view.loadUrl(url);
        ActionBar actionBar = getActionBar();
        //ActionBar actionBar = getSupportActionBar();
        actionBar.setLogo(R.drawable.logo);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}



-------------------

MainActivity.java


case R.id.bt12:



Java
@Override
   public void onClick(View v){
       switch(v.getId()) {

// Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://android-mocktest-portal.indianstudyspot.in/"));
startActivity(new Intent(this, WebViewMethod.class));
break;
}
Posted

To check if your device is internet connected or not use:
Java
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}

Use a permission in your Manifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And then use the method like:
Java
if (!isNetworkAvailable) {
    // write your toast message("Please check your internet connection")
} else {
    // code
}
 
Share this answer
 
v3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900