Using Snackbar you need to add android support design dependencies. Copy below dependencies and add to your build.gradle file.
Give an id to your layout. as I am using RelativeLayout so I give it an id like '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
Now inside onCreate() method write the following code to
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service; | |
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
/** | |
* Created by Sagor Sarker on 17-Nov-17. | |
*/ | |
public class ConnectionDetector { | |
Context context; | |
public ConnectionDetector(Context context) { | |
this.context = context; | |
} | |
public boolean isConnected() | |
{ | |
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE); | |
if(cm!=null){ | |
NetworkInfo info = cm.getActiveNetworkInfo(); | |
if(info!=null){ | |
if(info.getState() == NetworkInfo.State.CONNECTED) | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
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