How To Check Internet Connection

How To Check Internet Connection in Android Programmatically

In this tutorial, I will tell you how to find the availability of Internet Connection from your Android applications. This is a step-by-step Tutorial in the Java language. So, let’s learn How To Check Internet Connection in Android Studio programmatically.

How To Check Internet Connection in Android Programmatically:

 

1- So create a new Android Studio Project. Name it “CheckConnection”. Also, you can name it whatever you want. After the project builds you will have two files. MainActivity.kt and activity_main.xml.

2- After your project builds successfully. Then first come to your AndroidManifest.xml file. and add this permission.

 

Kotlin Check Internet Connection

 

activity_main.xml

 

3- Now come to your activity_main.xml file and change the default Constraint Layout to Relative Layout.

4- We will take a Button in our RelativeLayout. With match_parent width and wrap_content height. After adding some additional attributes our Button widget will be like this.

 

 

5- And our final activity_main file will look like this because we only have one button in our activity.

 

 

check internet connection android programmatically

 

MainActivity.java

 

6- Now. Come to your MainActivity.java file. And under the onCreate method initialize our Button widget.

 

7- Then. We will register an onClickListener on our Button.

8- Also inside our onClickListener method. We will create an object. Of ConnectivityManager class. We will use a System Service as well.

 

 

9- So here we add an if-else condition on our ConnectivityManager class object. Because we use the object to check if the Internet connection is available or not. Because if the connection would be available we will display our Toast message “Internet Available”.

 

 

10- And if the Internet is not available. We will display our Toast message “No Internet”.

11- So our final MainActivity.java file will look like this.

 

 

So, you can use this code anywhere in your program and learn how to programmatically check the availability of the Internet. You can use the code in an Alert Dialog for a better look don’t forget to add the Network State permission in your AndroidManifest.xml file.  Since I have used Button but you can use any other widgets of your choice. This is all for this tutorial :). Comment your Views below. And if you have any suggestions. So ask me.

So, practice Android Studio checks internet connection using this example and it works fine to check internet connection in Android kotlin. Also, it checks internet connection for Both wifi and Data (3G,4G, etc).

 

OUTPUT

 

How To Check Internet Connection
When. Internet Available.

 

How To Check Internet Connection
Data Connection. Is Available.

 

How To Check Internet Connection
When Internet turned Off.

 

       People Also Read

 

 

 

3 thoughts on “How To Check Internet Connection in Android Programmatically”

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!