How To Check Internet Connection

How To Check Internet Connection in Android Programmatically

In this tutorial. I will tell you how to check for Internet Connection availability from your Android applications. This is a step by step Tutorial in Java language. So, lets learn How To Check Internet Connection in Android Studio programmatically.

How To Check Internet Connection in Android Programmatically Kotlin:

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.

 

 

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.

 

MainActivity.java

 

6- Now. Come to your MainActivity.java file. And under 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 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 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, this example 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

 

 

 

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

  1. Pingback: Splash Screen Android Kotlin | Working Example Android - My Android

  2. Pingback: Kotlin Text to Speech Application | Working Example Kotlin - My Android

  3. Pingback: Check Internet Connection Programmatically Android Kotlin - My Android

  4. Pingback: Android Alert Dialog Example | Alert Dialog in Android Studio Example

  5. Pingback: Kotlin Speech To Text Example | Step by Step Example - My Android

  6. Pingback: Stylish Progress Bar In Android With Kotlin | Android Example - My Android

  7. Pingback: HorizontalScrollView Android Kotlin | Step By Step Example - My Android

  8. Pingback: Android Indeterminate Progress Bar: How to create Progress Bar

  9. Pingback: RecyclerView With Data Binding in Android Kotlin: Example With Kotlin

  10. Pingback: Kotlin Button OnClickListener | Android Studio Example - My Android

  11. Pingback: Multiple onClickListener In Kotlin | Android Studio Example - My Android

Leave a Comment

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