Skip to content

Instantly share code, notes, and snippets.

@neronguyenvn
Last active November 4, 2024 03:42
Show Gist options
  • Save neronguyenvn/8bd7eae41f66857f472592d7b0b8ebb7 to your computer and use it in GitHub Desktop.
Save neronguyenvn/8bd7eae41f66857f472592d7b0b8ebb7 to your computer and use it in GitHub Desktop.
Kotlin Throttled Click Extension for Views

Prevents multiple rapid clicks by disabling the view temporarily after a click, helping avoid unintended repeated actions.

fun View.onThrottledClick(
    throttleDelay: Long = 500L,
    onClick: (View) -> Unit
) {
    setOnClickListener {
        onClick(this)
        isClickable = false
        postDelayed({ isClickable = true }, throttleDelay)
    }
}

Usage

button.onThrottledClick {
    // Action to perform on click
}

Source

Inspired by Reddit discussion by k1llrogg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment