Simple library to check network connection status
Credit to https://stackoverflow.com/users/8187578/kebab-krabby
To detect connection changes, simply add a connection check to your activity's onCreate(Bundle savedInstanceState)
.
Add this to your project-level build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add this to your app-level build.gradle
dependencies {
implementation 'com.github.kaungkhantsoe:KConnectionCheckKotlin:TAG'
}
Simple usage
KConnectionCheck.addConnectionCheck(this, this, this);
Customize build
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
val builder = KConnectionCheck.CustomConnectionBuilder()
builder.bottomNavigationView = binding.bottomNavigation
KConnectionCheck.addConnectionCheck(
this,
this,
this,
builder
)
}
Detect status change
override fun onConnectionStatusChange(status: Boolean) {
// TODO: Here you can change ui according to network changes
}
setNoConnectionText(String noConnectionText)
setConnectionRestoredText(String connectionRestoredText)
setNoConnectionDrawable(@DrawableRes int noConnectionDrawable)
setConnectionRestoredDrawable(@DrawableRes int connectionRestoredDrawable)
hideWhenConnectionRestored(boolean hideWhenConnectionRestored) // Hide bottom snackbar on connection restored. Default is true
setNoConnectionTextColor(@ColorInt int noConnectionTextColor)
setConnectionRestoredTextColor(@ColorInt int connectionRestoredTextColor)
setDismissTextColor(@ColorInt int dismissTextColor)
setDismissText(String dismissText)
showSnackOnStatusChange(boolean showSnackOnStatusChange) // Show bottom snackbar on connection change. Default is true
setBottomNavigationView(@NonNull View bottomNavigationView); // Set BottomNavigationView for connection snack.