Check network connectivity on Android in 10 lines
Tags: android, network
Published: 2019-04-20 07:24:13

---
title: Check network connectivity on Android in 10 lines
published: true
tags: android, network
canonical_url: https://varunbarad.com/android/2018/02/08/check-network-connectivity-on-android-in-10-lines.html
---

Have you ever wanted to show different things to your user based on whether they are connected to the network or not? Well, Android provides a simple way to check for an active network connection.

Note: This is checking whether or not the device is connected to a network (and not whether it is connected to Internet or not).

The way Android provides to check for network connectivity is just 10 lines of code. But me being the lazy coder that I am, I just write that code inside a helper method once and then use that one-line method call everywhere that I need to check for connectivity.

As you can see in the code-block below, it is quite easy to wrap the connectivity check logic in a function.

```java
// Java code sample

public class ConnectivityHelper {
  public static boolean isConnectedToNetwork(Context context) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
    boolean isConnected = false;
    if (connectivityManager != null) {
      NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
      isConnected = (activeNetwork != null) && (activeNetwork.isConnectedOrConnecting());
    }
    
    return isConnected;
  }
}
```
```kotlin

// Kotlin code sample

// The next line should be the first statement in the file
@file:JvmName("ConnectivityHelper") // This line is only needed if you don't want caller statement in Java to change

fun Context.isConnectedToNetwork(): Boolean {
  val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
  return connectivityManager?.activeNetworkInfo?.isConnectedOrConnecting() ?: false
}
```

You also need to have the `ACCESS_NETWORK_STATE` permission added in your manifest like below.

```xml
<manifest ...>
  
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <application ...>
    ...
  </application>
</manifest>
```

### Done

Now anywhere that you need to check the connection, you only need to

```java
// Java code sample
if (ConnectivityHelper.isConnectedToNetwork(context)) {
  // Show the connected screen
} else {
  // Show disconnected screen
}
```
```kotlin
// Kotlin code sample
if (context.isConnectedToNetwork()) {
  // Show the connected screenn
} else {
  // Show disconnected screen
}
```

## Further reading

The [Official Documentations](https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html) are a good place to find more about this.

If you know of a better way for this, [contact me](https://varunbarad.com/contact) or tweet to me [@varun\_barad](https://twitter.com/varun_barad). You can even send me ideas for any other topics that you would like to know about.

### Notes

Hat-tip to [Giorgos Neokleous](https://dev.to/neokleoys2005) for suggesting to create an extension function in Kotlin.