Check if the Internet is Connected in Kotlin

Today we will discuss how to check Internet Connection availability from your Android applications. This is a step-by-step Tutorial in the Kotlin language. Let’s learn How To Check the Internet is Connected in Kotlin.

Determine the Status of Internet Connection

In android, we can determine the internet connection status easily by using the method of ConnectivityManager object.

Following is the code snippet of using in the ConnectivityManager class to know whether the internet connection is available or not.

 val ConnectionManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = ConnectionManager.activeNetworkInfo
            if (networkInfo != null && networkInfo.isConnected == true) {
                Toast.makeText(this@MainActivity, "Network Available", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this@MainActivity, "Network Not Available", Toast.LENGTH_LONG).show()
            }

Below we are going to create a simple Activity to determine internet connectivity. It contains a button and we check for connectivity when we click on it. In the below image you can see that in case of no Internet, a Toast message will appear “Network Not Available” by clicking on the button.

Internet is Not Connected

If the internet is connected, a message will display Network Available as you can see in the following image

Internet is Connected

Add the Permissions

First, come to your Android Manifest file and add all these permission in the AndroidManifest.xml file.

   <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

Activity_main XML File

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

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

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Handy Opinion"
        android:textSize="20dp"
        android:layout_marginTop="128dp"
        />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="178dp"
        android:text="CHECK INTERNET" />

</RelativeLayout>

Activity Code in Kotlin

Now, come to your MainActivity.kt file. So, We will use an if-else condition here. With the help of the activeNetworkInfo method, we will check for the current state of the NetworkInfo. If the NetworkInfo is in the connected state we will display a toast message “Network Available“. whereas if the NetworkInfo is not in the connected state then we will display a toast message “Network Not Available“.

package com.example.myapplication


import android.app.Activity
import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast

class MainActivity : Activity() {
    var btnNetworkcheck: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btnNetworkcheck = findViewById<View>(R.id.button1) as Button
        btnNetworkcheck!!.setOnClickListener {
            val ConnectionManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = ConnectionManager.activeNetworkInfo
            if (networkInfo != null && networkInfo.isConnected == true) {
                Toast.makeText(this@MainActivity, "Network Available", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this@MainActivity, "Network Not Available", Toast.LENGTH_LONG).show()
            }
        }
    }
}

So I hope now you understand how to check the Internet is Connected in Kotlin. You can use any message in your toast that you want to display. I have used a simple button to check the status of the internet connection with the click of the button. You can use this code anywhere in your app according to your need and do not forget to add the permission for accessing the Network_State in your Manifest file because it’s necessary permission.

If you have any questions, feel free to ask in the comments section below.

Next Article

Send Data to Server Using Google Volley in Android – Kotlin & Java

Please share this post:
Posts created 41

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: