Loading...

Check Internet Connection in Android

If you are making android application related to internet like news feed app, sms app, web app, etc then, it is better to check internet connectivity before doing other requests. Checking internet connectivity status is easy in android. Here I will show you to check internet connection in android (wifi and mobile data).

Related:
Android Elevation Example
How to Implement Ripple Effect in Android
Android Material Design with Design Support Library
Android Studio is Slow: How to Make Android Studio Fast

Create a new Android Project


Application Name: Checking Internet Connection
Company Domain: viralandroid.com
Package Name: com.viralandroid.checkinginternetconnection
Minimum SDK: Android 2.2 (API 8 Froyo)

Adding Internet and Access Network State Permission


After creating new project, add INTERNET and ACCESS_NETWORK_STATE in your AndroidManifest.xml file.

The final code of AndroidManifest.xml file looks like this.
AndroidManifest.xml

Modify XML Layout File


Now, add a button in your layout file called internet_connection.xml and add onClick attribute. Final code of xml layout looks like this.
res/layout/ internet_connection.xml

Java Activity File


Following is the final code of MainActivity.java file.
src/MainActivity.java

And default string resource.
res/values/strings.xml

Now, run your application and click the button which looks like this.

when internet connected in android phone

when internet is not connected in android phone

Tutorial 8916376008455813356
Home item