1

I have this code for a webview so that it shows when there's no connection in Android Studio it shows a message where there's no connection, then the user press a button to see if there's connection again, and it does it well but just the first time it opens. After the page loads with internet and you are navigating on if you deactivate the internet and enter any url it shows the 404 error again instead of showing the layout and the button to retry to connect to the internet.

Here is the code inside another fail created called NoConnection.java to have more order in the coding. It will be great if you tell me exactly what to put and where in the code because I'm kind of a newbie in Android Studio.

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class NoConnection extends AppCompatActivity {

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


    public void retry(View view){


        if(isNetworkAvailable() == true){
            Intent Intent = new Intent(NoConnection.this, SplaceScreen.class);
            startActivity(Intent);
        }else{
            Toast.makeText(this, "No internet connection.", Toast.LENGTH_LONG).show();
        }


    }



    public boolean isNetworkAvailable() {
        // Get Connectivity Manager class object from Systems Service
        ConnectivityManager cm = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);

        // Get Network Info from connectivity Manager
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();

        // if no network is available networkInfo will be null
        // otherwise check if we are connected
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }
}

Now, how can I modify it so that it works after the website loaded and you are navigating on it it shows the message?

Thanks in advance,

Henry
  • 17,490
  • 7
  • 63
  • 98

1 Answers1

0

The solution here is to check for isNetworkAvailable() every time you tap on a link (i.e, load a webpage). What you need to do is, listen to the urlLoad event in the WebView and before the load, call isNetworkAvailable().

You can do this by doing the following:

  1. Set a WebViewClient to the WebView.

  2. In your WebViewClient, override the shouldOverrideUrlLoading() method. This method will get called every time a url is loaded in the WebView. Call the isNetworkAvailable() inside this callback method. There is another callback method onPageStarted() that's invoked just before the page is loaded. You can try this method as well.

or

  1. You can try overriding the onReceivedError() method. This method will be called, when the url failed to load.

There are many ways to get this done, so see what fits you and go with that.

Henry
  • 17,490
  • 7
  • 63
  • 98
  • Can you please explain me more on the method number one. Cause in mainActivity.java there's already a WebViewClient. There's this: webView.setWebViewClient(new WebViewClient()). Is that what you are talking about? Can you please tell me the specific code to put and where to put it, I'm a noob. Oh and if I use onReceivedError how do I redirect it to my public class "NoConnection" so that it shows the button message and everything? – Sebastán Villalobos Aug 24 '18 at 02:18
  • You need to extend the `WebViewClient` and create your own `CustomWebViewClient`. Check this: https://stackoverflow.com/a/38484061/4747587 – Henry Aug 24 '18 at 02:21
  • Can you pleasetell me what to put exactly and where to solve the issue. As I say, I'm noob. Here there seems theres a character limit, but can you please put it on other answer if the code is big. – Sebastán Villalobos Aug 24 '18 at 02:41
  • Bro, I edited the question for you to check and if you can tell me what to add to solve that issue. – Sebastán Villalobos Aug 24 '18 at 04:50
  • I mean I know your answer is right, but I honestly I'm very noob and don't know how to do it, it'll be cool if you tell me what to put exactly on that code – Sebastán Villalobos Aug 24 '18 at 05:20
  • Just start the activity you want to, just like you did in the `retry()` method. – Henry Aug 24 '18 at 05:34
  • Could you please be my hero and edit it in other answer as it should be, please my friend. A quick copy paste of my code and what to add in bold letter real quick. Cause as I told you, I'm really noob and that code was provided by a friend, it just works well but doesn't have that function I need. – Sebastán Villalobos Aug 24 '18 at 05:42
  • Man can you please explain me better the OnReceivedError I think it will be easier for me. Can you please tell me which OnReceivedError to use and how, cause I've seen a couple and one is deprecated. – Sebastán Villalobos Aug 24 '18 at 08:47