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
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

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