-1

I know how to prevent double-clicking on a component. What I want to know if there is some way to avoid this behaviour "by default" when adding a component. Do I have to go every single item that can be clicked in an app and write the same code for every single component?

I know I could subclass, say, Button class, write the code for preventing double-clicking there, and only use my Button class in the app. What is the point of it? Why isn't something like that in the default Android Button class? I haven't seen any intended double-clicking behaviour in ANY app. Do I have to write a subclass of every single View that is susceptible of being clicked in order to prevent this? is there something I am missing?

Just to clarify, my question is about something like disableDoubleClick() method or something like that. I already said I know how to prevent this, but is a bit of a hassle to do it for every single clickable item in an app.

Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • I think Subclass is best solution for ur case.And many app's in android do have use cases for double click's so android will never restrict action's like that – Anmol Nov 07 '19 at 11:16
  • For my case...and for every single Android developer in the whole world... – Fustigador Nov 07 '19 at 11:18
  • I am not speaking about restriction. I am speaking about some kind of configuration (maybe, button.disableDoubleClick() or something like that). – Fustigador Nov 07 '19 at 11:20
  • Think in items in a RecyclerView. Those components, usually, are meant to react to a single click. Of course, not always, but in general that is the intended use. – Fustigador Nov 07 '19 at 11:22
  • If you don't write specific code of double tap then anyway's event will be ignored what's your issue here can you describe that ? – Anmol Nov 07 '19 at 11:22
  • No, its not ignored. If I double click fast on a item in a RecyclerView that opens another Activity, the second Activity opens twice. As I said, I know how to avoid that. But pisses my off to write the same double-click-preventing-thing in all lists in my app, and I have many of them. – Fustigador Nov 07 '19 at 11:28
  • And where I say RecyclerView, I mean Buttons, Images, TextViews, and whatnot – Fustigador Nov 07 '19 at 11:29
  • If that is your problem then it can be resolved just update it in you question i have a solution for multiple Activity getting triggered as it's not a issue off double click it's problem with delay in new activity getting created.I had the Same issue in my app but event's are not the cause for it – Anmol Nov 07 '19 at 11:33
  • I do have, too. My question was about a default disableDoubleClick mecanism I maybe missing, but doesn't looks like something like that exists. – Fustigador Nov 07 '19 at 11:35
  • Does this answer your question? [Android Preventing Double Click On A Button](https://stackoverflow.com/questions/5608720/android-preventing-double-click-on-a-button) – MichaelStoddart Nov 07 '19 at 11:36
  • For Multiple Activity on double click's refer this https://stackoverflow.com/a/52174030/7972699 – Anmol Nov 07 '19 at 11:39

1 Answers1

0

You can use Kotlin with its extensions in conjunction with reactive bindings of RxJava.

for the general view it will look something like:

fun View.clickWithDebounce(debounceTime: Long = 600L, action: () -> Unit): Disposable =
    RxView.clicks(this)
        .debounce(debounceTime, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe { action() }

Use it like this:

button.clickWithDebounce{
    //do stuff
}

textView.clickWithDebounce{
    //do other stuff
}

imageView.clickWithDebounce{
    //do some completely unrelated with the previous two stuff
}

floatingButton.clickWithDebounce(100000000){
    //do some stuff and wait 100000 seconds before the next click
}

For more info: Kotlin Extensions, RxJava RxBinding

Hope it helps.

Pavlo Ostasha
  • 14,527
  • 11
  • 35