Categories

Follow Us @web_learners

Friday, April 10, 2020

Check Internet Connection in Android App | Webview App Development Series Part 12

Hi Everyone, Let's continue with our webview app development series. The last post I have shown you How to upload files in Android Webview App. Till now we have added Progress Bar, Progress Dialog, Splash Screen, Pull to Refresh, Download, Upload and also some different functionality in our Webview App.

Today I am going to you How to check internet connection in our Webview App and also How to show no internet connection dialog when the internet connection is off.


First, we will design the layout in "activity_main.xml" file for showing no internet connection on our app.

For that, First of all, I will take a Relative layout inside our LinearLayout. Inside relative layout, we will take an Imageview, a Texview and a Button. 

In "ImageView" we will show our image. First I will copy my image and will paste onto "drawable" folder. Then we will write below code in activity_main.xml below the SwipeRefreshLayout which we have taken when we have done Pull to Refresh in our Webview App.

Code:


Then Below "ImageView" will take a TextView. Inside that textview we will display our text like "No Internet Connection". For that, we will take width, height, text alignment, text color, margin, id.



Code:


After that, We will take a button, the button text will "Retry" and will take id, height, margin and so on.

Code:


So the whole code will be in activity_main.xml is with SwipeRefreshLayout :

Code:


We have finished writing code in activity_main.xml.Now we will move to the "MainActivity.java". First, we will declare a Relative layout called "relativeLayout" and a button called "NointernetBtn".



Code:


After that, we will initialize them in onCreate.


Code:


Now we will write a function below onCreate. Inside that function, we will write conditions as if our mobile data is connected, then load our website in webview.

Else if wifi is connected then also load our website. Else show our no internet design what we have designed in the relative layout on activity_main.xml.


Code:


Now we will call our function "internetcheck()" in onCreate. After that, you can run your webview project, turn off your mobile data and then check you will see when mobile data or wifi is not connected it is showing our No internet page instead of the default error page.

Code:




Now we will write code for the "Retry" button in oncreate below where we have initialized our relative layout and button.


Code:


Now if you run your project again and click on the retry button, it will reload your website. As you can see we have written code for internet connection checking and it's working. And also if we click on the Retry button it is also successfully working.

But we have not fully done yet. If you notice suppose your Webview App is running then you just turn off your internet connection you will see it's showing you default error page not showing your custom error design. Because we have written function "internetcheck()", it will just check one time when our app will start. So, we will solve this problem also.

For that, We will call the override method "onReceivedError" in "WebViewClient". Inside it, will call internetcheck() function.


Code:


So the overall code will be in "MainActivity.java" with SwiperefreshLayout is:

Code:




That's it. We have done it successfully. In the Next Post, I will show you how to implement AdMob ads in Webview app. Stay tuned, Thanks.

1 comment:

  1. Hello friend, thanks for your tutorials and videos.

    I ask: how can I remove the swipRefreshlayout and enable just the retry button to conec internet.
    I have tried with different codes but it does not work, I need your help please.

    Thank you

    ReplyDelete