Android Jetpack Compose - Display Current Internet Connection Type
Last Updated :
19 Mar, 2025
Many times in android applications it is mandatory to check the user's mobile connectivity to the internet whether it may be through mobile data or using Wi-Fi so that the app will work fine with different internet connectivity sources.
In this article, we will be building a simple application in which we will be checking the real-time connection type of android devices using Jetpack Compose.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note: While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version.
Step 2: Adding permissions in the manifest file
Navigate to app > manifests > AndroidManifest.xml file and add the below permissions to it.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
MainActivity.kt:
Kotlin
package com.geeksforgeeks.demo
import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.geeksforgeeks.demo.ui.theme.DemoTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DemoTheme {
ConnectionChecker(LocalContext.current)
}
}
}
}
@Composable
fun ConnectionChecker(
context: Context,
) {
// on below line creating a variable
// for connection status.
val connectionType = remember {
mutableStateOf("Not Connected")
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(all = 30.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
// A Thread that will continuously
// monitor the Connection Type
Thread {
while (true) {
// Invoking the Connectivity Manager
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
// Fetching the Network Information
val netInfo = connectivityManager.allNetworkInfo
// on below line finding if connection
// type is wifi or mobile data.
for (networkInfo in netInfo) {
if (networkInfo.typeName.equals(
"WIFI",
ignoreCase = true
)
) if (networkInfo.isConnected) connectionType.value = "WIFI"
if (networkInfo.typeName.equals(
"MOBILE",
ignoreCase = true
)
) if (networkInfo.isConnected) connectionType.value = "MOBILE DATA"
}
}
}
// Starting the thread
.start()
Text(
text = "Network type:",
fontSize = 16.sp,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(20.dp))
Text(
text = connectionType.value,
color = Color.Black,
fontSize = 24.sp
)
}
}
Output: