Skip to content

Instantly share code, notes, and snippets.

@brh
Created July 23, 2020 22:58
Show Gist options
  • Save brh/be3222513ef7c9baa5f820020beedcaa to your computer and use it in GitHub Desktop.
Save brh/be3222513ef7c9baa5f820020beedcaa to your computer and use it in GitHub Desktop.
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.observe
import com.google.android.material.floatingactionbutton.FloatingActionButton
class NetworkConnectionFab: FloatingActionButton {
constructor(ctx: Context) : super(ctx)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, R.attr.floatingActionButtonStyle)
constructor(@NonNull context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
init {
setImageResource(R.drawable.ic_wifi_off)
size = SIZE_MINI
setOnClickListener {
Toast.makeText(context, R.string.no_network, Toast.LENGTH_SHORT).show()
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
NetworkDetectorLiveData.observe(context as LifecycleOwner){
if (it){
visibility = View.INVISIBLE
}
else {
visibility = View.VISIBLE
}
}
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment