Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent double code running by clicking twice fast to a button in Android

If i click fast to my button in my Android app, it seems that code behind it runs twice. If i click my menu button twice the activity that has to be launch onclick just starts twice and i have to quit from it twice.

This is really annoying because if i click too fast for menu buttons i can load up a whole bunch of activities in the background and i must quit them one by one, so this is clearly a buggy state of my app i want to fix this.

What can i do with this issue?

I use simple onClickListeners and Buttons

EDIT:

Regarding to answers and comments my menu buttons look like this:

top20Button.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {
        favButton.setClickable(false);
        nearButton.setClickable(false);
        highlightedButton.setClickable(false);
        top20Button.setClickable(false);

        Intent i = new Intent();
        i.putExtra("showDialog", false);
        i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        i.setClass(Search.this, Top20.class);
        startActivity(i);
        finish();

    }
});

After all this correction its still the same :S When i click like a mad person multiple activites are on the history stack and i must quit multiple times.

Any suggestions ? What m i doing wrong?

like image 559
Adam Varhegyi Avatar asked Jul 02 '12 09:07

Adam Varhegyi


People also ask

How do I restrict double click on Android?

Sometimes user clicks button too fast or double time, if button performs some kind of network operation, it'll call the function multiple times. To prevent double click, you can record the last time button clicked, and compare it to threshold of desired time.

Which activity is done on Start button click or double click?

Pressing the default mouse button twice quickly opens or executes a program or opens a file. For example, while in Microsoft Windows or most other operating systems double-clicking a program icon opens that program.


2 Answers

You can Disable the view temporarily like this

public static void disableTemporarly(final View view) {

        view.setEnabled(false);
        view.post(new Runnable() {
            @Override
            public void run() {

                view.setEnabled(true);
            }
        });
    }

Edit:

The above solution will work fine. But it will become even better when using the power of Kotlin

1) Create the SafeClikc Listener

class SafeClickListener(
        private var defaultInterval: Int = 1000,
        private val onSafeCLick: (View) -> Unit
) : View.OnClickListener {

    private var lastTimeClicked: Long = 0

    override fun onClick(v: View) {
        if (SystemClock.elapsedRealtime() - lastTimeClicked < defaultInterval) {
            return
        }
        lastTimeClicked = SystemClock.elapsedRealtime()
        onSafeCLick(v)
    }
}

2) Add extension function to make it works with any view, this will create a new SafeClickListener and delegate the work to it

fun View.setSafeOnClickListener(onSafeClick: (View) -> Unit) {

    val safeClickListener = SafeClickListener {
        onSafeClick(it)
    }
    setOnClickListener(safeClickListener)
}

3) Now it is very easy to use it

settingsButton.setSafeOnClickListener {
    showSettingsScreen()
}

Happy Kotlin ;)

like image 115
Simon K. Gerges Avatar answered Sep 30 '22 13:09

Simon K. Gerges


 Button.setOnClickListener(new View.OnClickListener {  
   @Override
   public void onClick(final View v) {
        v.setEnabled(false);
        v.postDelayed(new Runnable() {
         @Override
         public void run() {
            v.setEnabled(true);
         }
        },150); //150 is in milliseconds
    }
 });  
like image 23
anand krish Avatar answered Sep 30 '22 13:09

anand krish