Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Ensure single click on Android (ButterKnife did it right 👌)

 

 
Thanos Tsakiridis
Passionate Android developer @AsanaRebel. Always learning something new
Published: July 29, 2020
Tweet
Share
 

Photo by John Schnobrich on Unsplash

 

Recently, while I was investigating a weird issue of double network calls, I discovered that the root cause was not some bad network state or my HTTP client, but something much more simple and often overlooked. User double-tapping on a button too quickly…

A few years ago (like a lifetime in Android development) ButterKnife library was extremely popular for Java-based apps to bind views and avoid all the findViewById calls. It also used simple annotations like @OnClick to annotate methods and avoid setting up click listeners, I believe these are pretty known stuff. Now we use Kotlin synthetics (❤️) or View Binding , so ButterKnife got deprecated.

But as I mentioned ButterKnife had among others, this @OnClick annotation and it was hiding a small perk if you check the generated code: DebouncingClickListener

 

A View.OnClickListener click listener that debounces multiple clicks posted in the same frame. A click on one button disables all buttons for that frame.

 

What it did, was when you set up a click listener using the annotation, it automatically prevented for you accidental quick double clicks which may lead to weird behavior like the double network calls I was facing 😏 , or double pop up of dialogs, etc. but for all buttons that used this class as click handler! ButterKnife was indeed beautiful!

Of course, we can’t go back, but my idea was to copy and then extend the behavior with the help of Kotlin and extension methods.

First I created a DebouncingOnClickListener that was an exact copy of the original, translated to Kotlin. Then I added an extra parameter intervalMillis that can prevent clicks for a specified time period and not only for that frame.

 

 

And then an extension method to add this listener to my views:

 

 

I used a default value of 0 for the interval (which is the same as ButterKnife with disabling for one frame) so passing a value for the interval when you set the click listener at the view is totally optional. And the best is, that it will prevent clicks for all views that have this click handler!

And that’s it! No more double clicks! ✌️

 

 

Tags: Android, Kotlin, Butterknife, Android App Development, AndroidDev

 

View original article at: 


 

Originally published: July 10, 2020

Android News
Compose CameraX on Android
Compose CameraX on Android

By Peng Jiang

Android new UI toolkit Jetpack compose is in beta now, which has all the features you need to build production-ready apps. CameraX is another Jetpack support library, which let you control the camera easier. As compose is still under development, lots of the views are still not available the compose way.

By ProAndroidDev -
Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code With Kotlin Scopes
Noisy Code With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One