I\'m using two button in view. While clicking two button simultaneously it will goes to different activity at a time. How to avoid this?
I have tried like this, But
For Xamarin users, I have created a solution that subclasses the button class:
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using System;
using System.Threading.Tasks;
namespace MyProject.Droid.CustomWidgets
{
public class ButtonSingleClick : Button
{
private bool _clicked = false;
public int _timer = 700;
public new EventHandler Click;
protected ButtonSingleClick(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public ButtonSingleClick(Context context) : base(context)
{
base.Click += SingleClick;
}
public ButtonSingleClick(Context context, IAttributeSet attrs) : base(context, attrs)
{
base.Click += SingleClick;
}
public ButtonSingleClick(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
base.Click += SingleClick;
}
public ButtonSingleClick(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
base.Click += SingleClick;
}
private void SingleClick(object sender, EventArgs e)
{
if (!_clicked)
{
_clicked = true;
Click?.Invoke(this, e);
Task.Run(async delegate
{
await Task.Delay(_timer);
_clicked = false;
});
}
}
}
}
For Kotlin users
object AppUtil {
var mLastClickTime=0L
fun isOpenRecently():Boolean{
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return true
}
mLastClickTime = SystemClock.elapsedRealtime()
return false
}
}
In your Activity or Fragment or anywhere
just add this one line condition
if(isOpenRecently()) return
example:
fun startHomePage(activity: Activity){
if(isOpenRecently()) return //this one line enough
val intent= Intent(activity,MainActivity::class.java)
activity.startActivity(intent)
}
The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span). Example:
// Make your activity class to implement View.OnClickListener
public class MenuPricipalScreen extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
// setup listeners.
findViewById(R.id.imageView2).setOnClickListener(MenuPricipalScreen.this);
findViewById(R.id.imageView3).setOnClickListener(MenuPricipalScreen.this);
...
}
.
.
.
// variable to track event time
private long mLastClickTime = 0;
// View.OnClickListener.onClick method defination
@Override
public void onClick(View v) {
// Preventing multiple clicks, using threshold of 1 second
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
// Handle button clicks
if (v == R.id.imageView2) {
// Do your stuff.
} else if (v == R.id.imageView3) {
// Do your stuff.
}
...
}
.
.
.
}
if you are using kotlin then create extension fun as following:
fun View.clickWithDebounce(debounceTime: Long = 1200L, action: () -> Unit) {
this.setOnClickListener(object : View.OnClickListener {
private var lastClickTime: Long = 0
override fun onClick(v: View) {
if (SystemClock.elapsedRealtime() - lastClickTime < debounceTime) return
else action()
lastClickTime = SystemClock.elapsedRealtime()
}
})
}
Now on any view just call :
view.clickWithDebounce{
...
}
first :
public class ClickValidate {
public static long lastClickTime;
public static boolean isValid()
{
long current=System.currentTimeMillis();
long def = current - lastClickTime;
if (def>1000)
{
lastClickTime = System.currentTimeMillis();
return true;
}
else
return false;
}
}
Now just call this method everywhere in the body of onCLick method Or wherever you need:
if (ClickValidate.isValid()){
//your code
}
The simple way to do it in Kotlin is to use:
//When you need to disable the button
btn.isEnabled = false
//When you need to enable the button again
btn.isEnabled = true