How to use the LiveDataNetworkUtil library.
Tags: Livedata, Connectivity, Android, Kotlin

A simple demonstration of how the library works.

A few days ago, I published a simple lightweight library that observes network changes using a live data implementation. You can find it here. Today I will show you how to use it, by creating and implementing it in a very simple android application.

The first step is to create an android application. Then navigate to your root Gradle file and add this:

allprojects {

repositories {

...

maven { url 'https://jitpack.io' }

}

}

Then add the dependency in the app level Gradle file:

dependencies {

implementation 'com.github.ma-za-kpe:LiveDataNetworkUtil:0.2.1'

}

Build and sync the app. Then navigate to the newly created activity_main.xml file, inside a constraint layout, add the following.

<LinearLayout

android:id="@+id/networkStatusLayout"

android:layout_width="match_parent"

android:layout_height="50dp"

style="@style/StatusView"

android:background="@color/colorStatusNotConnected"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent">



<TextView

android:gravity="center"

android:id="@+id/textViewNetworkStatus"

style="@style/StatusTextView"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:text="@string/text_no_connectivity" />

</LinearLayout>

In your styles section, add the following themes:

<style name="StatusView">

<item name="android:orientation">vertical</item>

<item name="android:padding">8dp</item>

<item name="android:layout_gravity">top</item>

<item name="android:visibility">gone</item>

</style>



<style name="StatusTextView">

<item name="android:layout_gravity">center</item>

<item name="android:textColor">@android:color/white</item>

</style>

In colors.xml, add the following:

<color name="colorStatusConnected">#43A047</color>

<color name="colorStatusNotConnected">#D32F2F</color>

In the strings.xml file, add this:

<string name="text_no_connectivity">No Internet Connection, please connect …</string>

<string name="text_connectivity">Back Online</string>

Lastly, in the MainActivity.kt file, add this simple method that observes the network for internet status. Import the NetworkUtil into your activity file. Don't forget to call the handleNetwork() method, in your onCreate() method.

private fun handleNetwork() {

NetworkUtil.getNetworkLiveData(applicationContext).observe(this, { isConnected ->

if (!isConnected) {

mViewBinding.textViewNetworkStatus.text = getString(R.string.text_no_connectivity)

mViewBinding.networkStatusLayout.apply {

alpha = 0f

show()

setBackgroundColor(

ContextCompat.getColor(

context,

R.color.colorStatusNotConnected

))

animate()

.alpha(1f)

.setDuration(ANIMATION_DURATION)

.setListener(null)

}

} else {

mViewBinding.textViewNetworkStatus.text = getString(R.string.text_connectivity)

mViewBinding.networkStatusLayout.apply {

setBackgroundColor( ContextCompat.getColor(

context,

R.color.colorStatusConnected

))



animate()

.alpha(0f)

.setStartDelay(ANIMATION_DURATION)

.setDuration(ANIMATION_DURATION)

.setListener(object : AnimatorListenerAdapter() {

override fun onAnimationEnd(animation: Animator) {

hide()

}

})

}

}

})

} /**

* Companion object

*/

companion object {

const val ANIMATION_DURATION = 1000.toLong()

}

That's it!! Run the app. Then test the app by toggling the data icon on your phone. 👏

a video showcasing the use of this library.

All feedback is very welcome.😁

SOURCE CODE: here

Another implementation can be found here: MVVMNASAROVERAPP