Android Click Debounce

Android Click Debounce

As android platform is quite mature at this point, there are several approaches. For managing click events we can use external libraries or create our own implementation. Unfortunately ready solutions require us to use different api than basic onClickListener.

Sooner or later every developer comes across a problem with too many function calls in a short amount of time. A perfect example is user mashing the very same button which triggers multiple click events. The most popular solution is to introduce debouncing mechanism, which basically omits any calls in given time after previous invoke.

Image for post

As android platform is quite mature at this point, there are several approaches. For managing click events we can use external libraries or create our own implementation. Unfortunately ready solutions require us to use different api than basic onClickListener.

Databinding

There is a simple trick for those who use databinding. We can override android:onClick to make it use our own debouncing.

BindingAdapters.kt

@BindingAdapter("android:onClick")
fun setDebouncedListener(view: Button, onClickListener: View.OnClickListener) {
    val clickWithDebounce: (view: View) -> Unit = {

        /**
         *   add debounce logic here
         */

        onClickListener.onClick(it)
    }
    view.setOnClickListener(clickWithDebounce)
}

Create binding between activity and layout

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  val binding = DataBindingUtil.setContentView<ActivityMainBinding>(
    this, R.layout.activity_main
  )
binding.activity = this
binding.coroutineScope = lifecycleScope
}

data-binding kotlin android coroutine

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Kotlin Coroutines on Android - How to use Coroutines on Android

Coroutines are a Kotlin feature that convert async callbacks for long-running tasks, such as database or network access, into sequential code. This Kotlin Coroutines tutorial will show you how to use coroutines on Android, and how the new androidx-concurrent library makes it easy to use them to get things off the main thread. You'll also learn how the new library helps coroutines work with Architecture Components. This session also covers coroutine patterns, best practices, and even how to test coroutines!

Top Android Projects with Source Code

Android projects with source code - Work on real-time android projects. We’ll start project ideas from beginners level and later move to advance projects.

How to Develop an Android App with Kotlin?

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top Kotlin Android App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top-notch Kotlin App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!