Skip to content

Instantly share code, notes, and snippets.

companion object {
private const val timerDelay: Long = 10 * 1000 // 10 seconds in milliseconds
}
override fun onCreate(savedInstanceState: Bundle?) {
...
pager.adapter = ViewPagerAdapter(updates)
...
pager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
...
pager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
// Clear the messages so that every time an item has changed
// we reset our handler.
handler.removeMessages(0)
if (position == 0) btnBack.visibility = View.GONE
override fun onCreate(savedInstanceState: Bundle?) {
...
btnContinue.setOnClickListener {
if (pager.currentItem < pager.adapter?.itemCount!! - 1) {
handler.removeMessages(0)
pager.currentItem = ++pager.currentItem
}
}
// I expect the back button to be hidden when ViewPager2 is at position 0
@baggednismo
baggednismo / ViewLifecycleAware.kt
Created November 30, 2020 20:59 — forked from rishabhkohli/!!ViewLifecycleAware.kt
A Kotlin delegated property implementation which automatically clears itself at appropriate times in the View Lifecycle of a Fragment.
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
fun <T> Fragment.viewLifecycleAware(initialise: () -> T): ReadOnlyProperty<Fragment, T> =
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {
private var binding: T? = null
@baggednismo
baggednismo / PostalConversions.kt
Last active January 25, 2023 14:33
This class defines all the United States, Canada and Mexico states/provinces and allows for conversion from the full name to abbreviation.
import java.text.Normalizer
enum class State(val stateName: String) {
AK("Alaska"),
AL("Alabama"),
AR("Arkansas"),
AZ("Arizona"),
CA("California"),
CO("Colorado"),
CT("Connecticut"),