The Problem
Multiple clicks on a button bring a lot of unexpected/undesired output to the application’s functionality. We may have faced unexpected results when the button elements are clicked more than once before the click event responds. Recently I come through with a problem in an Android project, where the end-user faced multiple entries on his application. To prevent multiple clicks on a button in Android I have tried a way that helps me to avoid undesired results in the application flow.
Solution for Rapid Click
In this read, we will see how to prevent multiple clicks in the android mobile application. To provide a solution to this I went through the events log and noticed the user clicking the button continously. So always try to debug your codings to confirm whether it happened from the UI part or your code. If the problem exists in your code then fix it to avoid the problem.
At some point, we can’t expect the user to use the application with normal actions. They may use the application in the way they like to do. If the device is not responding properly then it will be the worst scenario for the app flow. The user may click it multiple times and the application may crash or open the screens multiple times or result in a big issue if it is related to the financial application.
When the application opens a screen multiple times it’s spoiling the flow of the application and the user experience, so it is important to stop such problems earlier.
Also Read: Android SearchView in ActionBar— Androidx and Kotlin
To prevent such problems it is important to handle the touch events in the application to prevent any unexpected results in data. Here I got a solution for such an issue by using the Handler. The handler is used to schedule messages and to be executed at some point, and to enqueue an action to be performed on a different thread. For more please refer below link
Solution for Rapid Click Issue on a Button
Please use the following in code where the button event is called, and do your logic inside the click event listener.
The above code is run when the button is clicked in the UI, and disable the button clicked event for the given interval time. After disabling the button it is a better way to notify the user that the button is disabled, so assign alpha to 0.5
to the button so the user will identify the button is disabled. The Handler()
is the Kotlin method that helps to run threads with Runnable
. In this example, I have used 1500
milli seconds as the interval for a better experience. You can use any value as you wish, but keep in mind that if the call is not to return or show any result the user needs to click it again, after this time interval the button should be enabled and the alpha
set to 1
.
That’s it, below is the outcome of this method. Here you can see that the button is disabled for some time when the click is listened to. And again it enabled to handle the click event. In this way, you can prevent the multiple click event easily.
Hint: You can use the progress bar if the click event takes some time to process. If it’s an API call, use full screen and non-cancelable dialogue to prevent user interaction with the UI. And disable the progress bar from the program to make further interactions; once the API call or background process is completed.
Also, read on Medium and support.
Comments 1