Oops! No Internet!

Additional

Language
Kotlin
Version
v2.0.0 (Jan 20, 2021)
Created
Dec 8, 2019
Updated
Dec 12, 2023
Owner
Md. Mahmudul Hasan Shohag (ImaginativeShohag)
Contributor
Md. Mahmudul Hasan Shohag (ImaginativeShohag)
1
Activity
Badge
Generate
Download
Source code

Oops! No Internet!

Simple no Internet dialogs and snackbar, which will automatically appear and disappear based on Internet connectivity status.

Previews

Preview of NoInternetDialogSignal:

Day Mode Day Mode (Airplane Mode) Night Mode

Preview of NoInternetDialogPendulum:

Day Mode Day Mode (Airplane Mode) Night Mode

Preview of NoInternetSnackbarFire:

Day Mode Day Mode (Airplane Mode) Night Mode

Types

The library provides NoInternetObserveComponent, a simple lifecycle-aware component. The purpose of the component is to provide an easy way to notify about the Internet connection connectivity change. It gives a simple interface to connect with any elements (Dialog, Snackbar, etc.) with ease. You can also use the BaseNoInternetDialog class to easily create custom no Internet dialog. The library currently provides two Dialogs and a Snackbar out of the box for no Internet notification.

No Internet Dialogs

  • NoInternetDialogPendulum (formerly just NoInternetDialog)
  • NoInternetDialogSignal

No Internet Snackbar

  • NoInternetSnackbarFire

Usage

Dependency

Add the followings to your project level build.gradle file.

android {

    // ...

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    // ...
    
    // Material Components for Android. Replace the version with the latest version of Material Components library.
    implementation 'com.google.android.material:material:1.2.1'

    implementation 'org.imaginativeworld.oopsnointernet:oopsnointernet:2.0.0'
}

Note 0. Minimum SDK for this library is API 21 (Android 5.0 Lollipop).

Note 1. Your application has to use AndroidX to use this library.

Note 2. You have to use *.MaterialComponents.* in your styles.

Finally

Just initialize any of the NoInternetDialog* and/or NoInternetSnackbar* using the builder in onCreate(), that's all! :)

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 examples.

// Kotlin
class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // ...

        // No Internet Dialog: Pendulum
        NoInternetDialogPendulum.Builder(
            this,
            lifecycle
        ).apply {
            dialogProperties.apply {
                connectionCallback = object : ConnectionCallback { // Optional
                    override fun hasActiveConnection(hasActiveConnection: Boolean) {
                        // ...
                    }
                }

                cancelable = false // Optional
                noInternetConnectionTitle = "No Internet" // Optional
                noInternetConnectionMessage =
                "Check your Internet connection and try again." // Optional
                showInternetOnButtons = true // Optional
                pleaseTurnOnText = "Please turn on" // Optional
                wifiOnButtonText = "Wifi" // Optional
                mobileDataOnButtonText = "Mobile data" // Optional

                onAirplaneModeTitle = "No Internet" // Optional
                onAirplaneModeMessage = "You have turned on the airplane mode." // Optional
                pleaseTurnOffText = "Please turn off" // Optional
                airplaneModeOffButtonText = "Airplane mode" // Optional
                showAirplaneModeOffButtons = true // Optional
            }
        }.build()

        // No Internet Dialog: Signal
        NoInternetDialogSignal.Builder(
            this,
            lifecycle
        ).apply {
            dialogProperties.apply {
                connectionCallback = object : ConnectionCallback { // Optional
                    override fun hasActiveConnection(hasActiveConnection: Boolean) {
                        // ...
                    }
                }

                cancelable = false // Optional
                noInternetConnectionTitle = "No Internet" // Optional
                noInternetConnectionMessage =
                "Check your Internet connection and try again." // Optional
                showInternetOnButtons = true // Optional
                pleaseTurnOnText = "Please turn on" // Optional
                wifiOnButtonText = "Wifi" // Optional
                mobileDataOnButtonText = "Mobile data" // Optional

                onAirplaneModeTitle = "No Internet" // Optional
                onAirplaneModeMessage = "You have turned on the airplane mode." // Optional
                pleaseTurnOffText = "Please turn off" // Optional
                airplaneModeOffButtonText = "Airplane mode" // Optional
                showAirplaneModeOffButtons = true // Optional
            }
        }.build()
        
        // No Internet Snackbar: Fire
        NoInternetSnackbarFire.Builder(
            binding.mainContainer,
            lifecycle
        ).apply {
            snackbarProperties.apply {
                connectionCallback = object : ConnectionCallback { // Optional
                    override fun hasActiveConnection(hasActiveConnection: Boolean) {
                        // ...
                    }
                }

                duration = Snackbar.LENGTH_INDEFINITE // Optional
                noInternetConnectionMessage = "No active Internet connection!" // Optional
                onAirplaneModeMessage = "You have turned on the airplane mode!" // Optional
                snackbarActionText = "Settings" // Optional
                showActionToDismiss = false // Optional
                snackbarDismissActionText = "OK" // Optional
            }
        }.build()
    }
}
// Java
public class Main2Activity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // ...
        
        // No Internet Dialog: Pendulum
        NoInternetDialogPendulum.Builder builder = new NoInternetDialogPendulum.Builder(
            this,
            getLifecycle()
        );

        DialogPropertiesPendulum properties = builder.getDialogProperties();

        properties.setConnectionCallback(new ConnectionCallback() { // Optional
            @Override
            public void hasActiveConnection(boolean hasActiveConnection) {
                // ...
            }
        });

        properties.setCancelable(false); // Optional
        properties.setNoInternetConnectionTitle("No Internet"); // Optional
        properties.setNoInternetConnectionMessage("Check your Internet connection and try again"); // Optional
        properties.setShowInternetOnButtons(true); // Optional
        properties.setPleaseTurnOnText("Please turn on"); // Optional
        properties.setWifiOnButtonText("Wifi"); // Optional
        properties.setMobileDataOnButtonText("Mobile data"); // Optional

        properties.setOnAirplaneModeTitle("No Internet"); // Optional
        properties.setOnAirplaneModeMessage("You have turned on the airplane mode."); // Optional
        properties.setPleaseTurnOffText("Please turn off"); // Optional
        properties.setAirplaneModeOffButtonText("Airplane mode"); // Optional
        properties.setShowAirplaneModeOffButtons(true); // Optional

        builder.build();
        
        // No Internet Dialog: Signal
        NoInternetDialogSignal.Builder builder = new NoInternetDialogSignal.Builder(
            this,
            getLifecycle()
        );

        DialogPropertiesSignal properties = builder.getDialogProperties();

        properties.setConnectionCallback(new ConnectionCallback() { // Optional
            @Override
            public void hasActiveConnection(boolean hasActiveConnection) {
                // ...
            }
        });

        properties.setCancelable(false); // Optional
        properties.setNoInternetConnectionTitle("No Internet"); // Optional
        properties.setNoInternetConnectionMessage("Check your Internet connection and try again"); // Optional
        properties.setShowInternetOnButtons(true); // Optional
        properties.setPleaseTurnOnText("Please turn on"); // Optional
        properties.setWifiOnButtonText("Wifi"); // Optional
        properties.setMobileDataOnButtonText("Mobile data"); // Optional

        properties.setOnAirplaneModeTitle("No Internet"); // Optional
        properties.setOnAirplaneModeMessage("You have turned on the airplane mode."); // Optional
        properties.setPleaseTurnOffText("Please turn off"); // Optional
        properties.setAirplaneModeOffButtonText("Airplane mode"); // Optional
        properties.setShowAirplaneModeOffButtons(true); // Optional

        builder.build();
        
  // No Internet Snackbar: Fire
        NoInternetSnackbarFire.Builder builder = new NoInternetSnackbarFire.Builder(
            binding.mainContainer,
            getLifecycle()
        );

        SnackbarPropertiesFire properties = builder.getSnackbarProperties();

        properties.setConnectionCallback(new ConnectionCallback() { // Optional
            @Override
            public void hasActiveConnection(boolean hasActiveConnection) {
                // ...
            }
        });

        properties.setDuration(Snackbar.LENGTH_INDEFINITE); // Optional
        properties.setNoInternetConnectionMessage("No active Internet connection!"); // Optional
        properties.setOnAirplaneModeMessage("You have turned on the airplane mode!"); // Optional
        properties.setSnackbarActionText("Settings"); // Optional
        properties.setShowActionToDismiss(false); // Optional
        properties.setSnackbarDismissActionText("OK"); // Optional

        builder.build();
    }
}

The sample project can be found here.

Credits

This library is inspired by NoInternetDialog.

Library name credit goes to Fahima Lamia Neha.

The icons and images used in the sample project are from freepik.com and flaticon.com.

Changelog

2.0.0

We move our library from jitpack.io to maven repository. So no need to add any repositories for using the library.

We changed the file structures and re-right the whole library. So you have to do a little refactoring to use version 2 of the library. Just follow the samples, you will get it. Or add an issue if there any problem to migrate.

The library now provides a simple lifecycle-aware component for observing the Internet connection changes. It can be used with any kind of custom elements.

We provide two out of the box dialogs and a snackbar implementation.

Notable other changes:

  • New: NoInternetObserveComponent lifecycle-aware component added.
  • New: Custom no Internet dialogs can be easily created using BaseNoInternetDialog class.
  • Changed: Previous NoInternetDialog now NoInternetDialogPendulum.
  • New: New NoInternetDialogSignal dialog added.
  • Removed: NoInternetSnackbar removed.
  • New: New NoInternetSnackbarFire added.
  • New: All the dialogs and snackbar are dark-mode ready.

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

Icons made by Darius Dan from www.flaticon.com
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.