Updated:
To check the internet connection in Android with Kotlin, use ConnectivityManager with NetworkCapabilities. For a one-time check, read the active network’s capabilities. To watch the connection continuously, register a NetworkCallback and expose it as a Kotlin Flow. This guide shows both, plus how to show an “offline” message in an XML Activity or Jetpack Compose.
📘 New to Kotlin? Learn it step by step with our free Kotlin course: 51 short lessons with runnable examples, from your first program to coroutines.
Updated for 2026: older tutorials (including earlier versions of this post) use activeNetworkInfo, isConnected or a BroadcastReceiver for CONNECTIVITY_ACTION. Those are deprecated. CONNECTIVITY_ACTION was deprecated in API 28, and Google recommends registerDefaultNetworkCallback instead (ConnectivityManager reference).


Step 1: Add the permission
In AndroidManifest.xml, add the network state permission. It is a normal permission, so there is no runtime prompt.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Step 2: One-time check – is the device online right now?
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
fun Context.isOnline(): Boolean {
val cm = getSystemService(ConnectivityManager::class.java)
val caps = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false
return caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}Why two checks? NET_CAPABILITY_INTERNET only means the network is set up for internet. NET_CAPABILITY_VALIDATED means Android actually reached the internet through it. For example, hotel or airport Wi-Fi with a login page has the first but not the second.
Step 3: Check continuously with a Flow
This class emits true or false whenever the connection changes. It uses registerDefaultNetworkCallback (API 24+), which follows the network the system is using for your app.
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.distinctUntilChanged
class ConnectivityObserver(context: Context) {
private val cm = context.getSystemService(ConnectivityManager::class.java)
val isOnline: Flow<Boolean> = callbackFlow {
val callback = object : ConnectivityManager.NetworkCallback() {
override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) {
trySend(
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
)
}
override fun onLost(network: Network) {
trySend(false)
}
}
// Current state first, so the UI doesn't wait for a change
val caps = cm.getNetworkCapabilities(cm.activeNetwork)
trySend(caps?.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) == true)
cm.registerDefaultNetworkCallback(callback)
awaitClose { cm.unregisterNetworkCallback(callback) }
}.distinctUntilChanged()
}awaitClose unregisters the callback when nobody is collecting, so you don’t leak it.
Step 4a: Show an offline message in an Activity (XML views)
Add a hidden banner to your layout:
<TextView
android:id="@+id/offlineBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D32F2F"
android:gravity="center"
android:padding="8dp"
android:text="No internet connection"
android:textColor="#FFFFFF"
android:visibility="gone" />Then collect the Flow while the Activity is visible:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val banner = findViewById<TextView>(R.id.offlineBanner)
val observer = ConnectivityObserver(applicationContext)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
observer.isOnline.collect { online ->
banner.isVisible = !online
}
}
}
}
}repeatOnLifecycle needs androidx.lifecycle:lifecycle-runtime-ktx, and isVisible comes from androidx.core:core-ktx. New Android Studio projects include both.
Step 4b: Show an offline banner in Jetpack Compose
@Composable
fun OfflineBanner(observer: ConnectivityObserver) {
val online by observer.isOnline.collectAsStateWithLifecycle(initialValue = true)
AnimatedVisibility(visible = !online) {
Text(
"No internet connection",
color = Color.White,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.background(Color(0xFFD32F2F))
.padding(8.dp)
)
}
}In a real app, create one ConnectivityObserver (for example in your Application class or with Hilt) and share it, rather than creating one per screen.
Migrating from BroadcastReceiver, LiveData or NetworkInfo
| Old code | Replace with |
|---|---|
cm.activeNetworkInfo?.isConnected | isOnline() from Step 2 |
BroadcastReceiver + ConnectivityManager.CONNECTIVITY_ACTION | registerDefaultNetworkCallback (Step 3) |
NetworkInfo.type == TYPE_WIFI | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) |
LiveData<Boolean> connection class | The Flow in Step 3 (or .asLiveData() if you still need LiveData) |
Wi-Fi or mobile data?
fun Context.connectionType(): String {
val cm = getSystemService(ConnectivityManager::class.java)
val caps = cm.getNetworkCapabilities(cm.activeNetwork) ?: return "None"
return when {
caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> "Wi-Fi"
caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> "Mobile data"
caps.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> "Ethernet"
else -> "Other"
}
}How to test it
- On the emulator, toggle Wi-Fi and mobile data in quick settings, or use Extended controls → Cellular.
- On a phone, turn on airplane mode and back off.
- Remember that “connected” doesn’t guarantee a request will succeed. Still handle network errors (timeouts,
IOException) in your API calls.
FAQ
Is NetworkInfo deprecated?
Yes. activeNetworkInfo and NetworkInfo are deprecated (API 29); use NetworkCapabilities.
Do I need a BroadcastReceiver to detect network changes?
No. A NetworkCallback registered with registerDefaultNetworkCallback replaces it and works while your app is running.
Why does it say online when the internet doesn’t work?
Check NET_CAPABILITY_VALIDATED, not just NET_CAPABILITY_INTERNET. A network can be connected but blocked by a login page or have no real access.
More Android guides
Keep reading



