Android Detect Connected Network type in Android Studio using Kotlin.

Last updated Dec 18, 2021

In this android example, we will see how to Detect Connected Network Type either Mobile or WIFI in Android Studio by using Kotlin Language.

Implementation:

Step 1: Create a new Project in android studio.

Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish

 

Step 2: Go to activity_main.xml file and add the following code

   <LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Current Network Type"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <Button
        android:id="@+id/checkBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Check"></Button>
</LinearLayout>

 

Step 3:  Add permission to the AndroidManifest.xml file above tag.

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

 

Step 4: Open MainActivity.kt file and add the following code.

 lateinit var type : TextView
    lateinit var check: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        type = findViewById(R.id.type)
        check = findViewById(R.id.checkBtn)

        check.setOnClickListener{
            if (checkNetwork()) {
                Toast.makeText(this, "Connected With Network", Toast.LENGTH_SHORT).show()
            }
            else if (!checkNetwork()) {
                type.text = "Not Connected"
                Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show()
            }
        }

    }
    private fun checkNetwork(): Boolean {
        val connManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connManager.activeNetworkInfo
        if(networkInfo!= null){
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                type.text = networkInfo.typeName
                // connected to wifi
                return true;
            } else{
                networkInfo.getType() == ConnectivityManager.TYPE_MOBILE
                type.text = networkInfo.typeName
                return true
            };
        }

//        type.text = networkInfo!!.typeName
        return false
    }

 

Step 5: Run the app on emulator or real device, you will get the output as in Video


Complete Source Code of Detect Connected Network type Android Example 
 

activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Current Network Type"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <Button
        android:id="@+id/checkBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Check"></Button>
</LinearLayout>

 

MainActivity.kt file

import android.content.Context
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    lateinit var type : TextView
    lateinit var check: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        type = findViewById(R.id.type)
        check = findViewById(R.id.checkBtn)

        check.setOnClickListener{
            if (checkNetwork()) {
                Toast.makeText(this, "Connected With Network", Toast.LENGTH_SHORT).show()
            }
            else if (!checkNetwork()) {
                type.text = "Not Connected"
                Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show()
            }
        }

    }
    private fun checkNetwork(): Boolean {
        val connManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connManager.activeNetworkInfo
        if(networkInfo!= null){
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                type.text = networkInfo.typeName
                // connected to wifi
                return true;
            } else{
                networkInfo.getType() == ConnectivityManager.TYPE_MOBILE
                type.text = networkInfo.typeName
                return true
            };
        }

//        type.text = networkInfo!!.typeName
        return false
    }
}

 

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.networkType">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.XmlParser">
        <activity
            android:name=".MainActivity"
            android:exported="true">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>
</manifest>

 

Conclusion: In this article we have covered how to detect connected network type in Android Studio by using Kotlin Language.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Current Network Type"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <Button
        android:id="@+id/checkBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Check"></Button>
</LinearLayout>

 

MainActivity.kt file

import android.content.Context
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    lateinit var type : TextView
    lateinit var check: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        type = findViewById(R.id.type)
        check = findViewById(R.id.checkBtn)

        check.setOnClickListener{
            if (checkNetwork()) {
                Toast.makeText(this, "Connected With Network", Toast.LENGTH_SHORT).show()
            }
            else if (!checkNetwork()) {
                type.text = "Not Connected"
                Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show()
            }
        }

    }
    private fun checkNetwork(): Boolean {
        val connManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connManager.activeNetworkInfo
        if(networkInfo!= null){
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                type.text = networkInfo.typeName
                // connected to wifi
                return true;
            } else{
                networkInfo.getType() == ConnectivityManager.TYPE_MOBILE
                type.text = networkInfo.typeName
                return true
            };
        }

//        type.text = networkInfo!!.typeName
        return false
    }
}

 

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.networkType">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.XmlParser">
        <activity
            android:name=".MainActivity"
            android:exported="true">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>
</manifest>

 

Conclusion: In this article we have covered how to detect connected network type in Android Studio by using Kotlin Language.

-->
Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

578 Views