Check Internet Connection Programmatically

Check Internet Connection Programmatically Android Kotlin

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 Kotlin language. Lets learn How To Check Internet Connection Programmatically In Android Kotlin.

 

Check Internet Connection Programmatically In Android With Kotlin:

1- Create a new Android Studio Project. Name it “Check_Internet_Connection” or you can name it whatever you want.

2- After your project builds successfully, first come to your Android Manifest file and add this permission. 

3- Now, come to your activity_main.xml file. Change your default Constraint Layout to Relative Layout.

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

5- And after that our final activity_main file will look like this.

 

KOTLIN PART :

6- Now, come to your MainActivity.kt file. So, We will take three variable here like this :

7- After that, we will register an onClickListener on our Button. check_connection is the id of our button in xml. We will make use of a System Service here.

 

8- We will use an if else condition here. With the help of activeNetworkInfo method we will check for the current state of the NetworkInfo. If the NetworkInfo is in connected state we will display a toast message CONNECTED. whereas if the NetworkInfo is not in the connected state then we will display a toast message ” NOT CONNECTED “.

 

9- And Our complete and final MainActivity.kt will be like this.

 

You can use any message in your toast that you want to display. I have used a simple button to check the status of internet connection on click of the button. You can use any other widget or icon according to your need. And you can use this code anywhere in your app according to your need. Do not forget to add the permission for accessing the Network_State in your Manifest file because its a must permission.

That’s all for this tutorial. 🙂 …

 

OUTPUT

When No Internet Connection is Available 

Check Internet Connection Programmatically Android Kotlin
Check Internet Connection Programmatically

 

Wifi Icon enabled because Wifi connection is available and connected.

Check Internet Connection Programmatically Android Kotlin
Check Internet Connection Programmatically

 

When 3G OR 4G is Available

Check Internet Connection Programmatically Android Kotlin

 

 

 People Also Read:

Splash Screen Android Kotlin Working Example Android

How To Check Internet Connection in Android Programmatically With Java

AlertDialog Android Kotlin | AlertDialog in Kotlin with Example

RecyclerView With Data Binding in Android Kotlin: Example With Kotlin

 

2 thoughts on “Check Internet Connection Programmatically Android Kotlin”

  1. Pingback: Key Differences Between JavaScript And Python - My Android

  2. Pingback: Kotlin vs java | Which One Is Better? Details and Comparison - My Android

Leave a Comment

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