Oops! No Internet!
A simple no Internet dialog and snackbar, which will automatically
appear and disappear based on Internet connectivity status.

Previews
Usage
Dependency
Step 1. Add the JitPack repository to your build file
Add it to your root build.gradle at the end of repositories:
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
// Material Components for Android
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.github.ImaginativeShohag:Oops-No-Internet:v1.1.5'
}
Note 0. Minimum SDK for this library is API 21 (Android 5.0 Lollipop).
Note 1. Your application have to use AndroidX to use this library.
Note 2. Your have to use *.MaterialComponents.* in you styles.
Finally
To use the NoInternetDialog and/or NoInternetSnackbar, use the builder and initialize it in onResume() and finally
destroy it in onPause().
The NoInternetDialog and/or NoInternetSnackbar will then automatically appear when no active Internet connection found and disappear otherwise.
Customizable attributes with their default values are given in the following example.
class MainActivity : AppCompatActivity() {
private var noInternetDialog: NoInternetDialog? = null
private var noInternetSnackbar: NoInternetSnackbar? = null
override fun onResume() {
super.onResume()
noInternetDialog = NoInternetDialog.Builder(this)
.apply {
connectionCallback = object : ConnectionCallback {
override fun hasActiveConnection(hasActiveConnection: Boolean) {
}
}
cancelable = false
noInternetConnectionTitle = "No Internet"
noInternetConnectionMessage =
"Check your Internet connection and try again."
showInternetOnButtons = true
pleaseTurnOnText = "Please turn on"
wifiOnButtonText = "Wifi"
mobileDataOnButtonText = "Mobile data"
onAirplaneModeTitle = "No Internet"
onAirplaneModeMessage = "You have turned on the airplane mode."
pleaseTurnOffText = "Please turn off"
airplaneModeOffButtonText = "Airplane mode"
showAirplaneModeOffButtons = true
}
.build()
noInternetSnackbar =
NoInternetSnackbar.Builder(this, findViewById(android.R.id.content))
.apply {
connectionCallback = object : ConnectionCallback {
override fun hasActiveConnection(hasActiveConnection: Boolean) {
}
}
indefinite = true
noInternetConnectionMessage = "No active Internet connection!"
onAirplaneModeMessage = "You have turned on the airplane mode!"
snackbarActionText = "Settings"
showActionToDismiss = false
snackbarDismissActionText = "OK"
}
.build()
}
override fun onPause() {
super.onPause()
noInternetDialog?.destroy()
noInternetSnackbar?.destroy()
}
}
public class Main2Activity extends AppCompatActivity {
NoInternetDialog noInternetDialog;
NoInternetSnackbar noInternetSnackbar;
@Override
protected void onResume() {
super.onResume();
NoInternetDialog.Builder builder1 = new NoInternetDialog.Builder(this);
builder1.setConnectionCallback(new ConnectionCallback() {
@Override
public void hasActiveConnection(boolean hasActiveConnection) {
}
});
builder1.setCancelable(false);
builder1.setNoInternetConnectionTitle("No Internet");
builder1.setNoInternetConnectionMessage("Check your Internet connection and try again");
builder1.setShowInternetOnButtons(true);
builder1.setPleaseTurnOnText("Please turn on");
builder1.setWifiOnButtonText("Wifi");
builder1.setMobileDataOnButtonText("Mobile data");
builder1.setOnAirplaneModeTitle("No Internet");
builder1.setOnAirplaneModeMessage("You have turned on the airplane mode.");
builder1.setPleaseTurnOffText("Please turn off");
builder1.setAirplaneModeOffButtonText("Airplane mode");
builder1.setShowAirplaneModeOffButtons(true);
noInternetDialog = builder1.build();
NoInternetSnackbar.Builder builder2 = new NoInternetSnackbar.Builder(this, (ViewGroup) findViewById(android.R.id.content));
builder2.setConnectionCallback(new ConnectionCallback() {
@Override
public void hasActiveConnection(boolean hasActiveConnection) {
}
});
builder2.setIndefinite(true);
builder2.setNoInternetConnectionMessage("No active Internet connection!");
builder2.setOnAirplaneModeMessage("You have turned on the airplane mode!");
builder2.setSnackbarActionText("Settings");
builder2.setShowActionToDismiss(false);
builder2.setSnackbarDismissActionText("OK");
noInternetSnackbar = builder2.build();
}
@Override
protected void onPause() {
super.onPause();
if (noInternetDialog != null) {
noInternetDialog.destroy();
}
if (noInternetSnackbar != null) {
noInternetSnackbar.destroy();
}
}
}
The sample project can be found here.
Credits
This library is inspired by NoInternetDialog.
Library name credit goes to Fahima Lamia Neha.
Changelog
1.1.4 & 1.1.5
NoInternetUtils functions are now documented.
- Dialog width optimized for small screens.
- Dependency updated.
1.1.3
NoInternetUtils class methods are now accessible independently.
1.1.2
Airplane animation tweak.
1.1.1
Small change in check internet.
1.1.0
+ Airplane mode button added to the dialog.
+ The snackbar text will be updated based on airplane mode.
1.0.1
Airplane mode animation added to the dialog and known bugs fixed.
1.0.0
The initial release of the library.
Licence
Copyright 2019 Md. Mahmudul Hasan Shohag
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.