Wednesday, November 22, 2017

Checking internet connection and showing result in Snackbar in android studio

Using Snackbar you need to add android support design dependencies. Copy below dependencies and add to your build.gradle file.


compile 'com.android.support:design:25.3.1'

Give an id to your layout. as I am using RelativeLayout so I give it an id like 'mylayout'

android:id="@+id/mylayout"

In MainActivity.java file inside onCreate() method added your layout id like




RelativeLayout rel = (RelativeLayout) findViewById(R.id.mainactivity);

Now for checking internet connection create a java file and name it like ConnectionDetection.java



On MainActivity. java create

private ConnectionDetector cd;
...........
Now inside onCreate() method write the following code to

cd = new ConnectionDetector(this);

if(!cd.isConnected()){
    Snackbar snackbar1 = Snackbar.make(mainActivity,
 "No Internet Connection,Please Connect", Snackbar.LENGTH_LONG);
    snackbar1.show();
}




Run Your Application without internet connection

No comments:

Post a Comment

Recent Post