0

I am using following code for using a button. I works.(sendBtn is a button in a fragment)

sendText = view.findViewById(R.id.send_text);
    View sendBtn = view.findViewById(R.id.send_btn);
        sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));

Now i want to disable the button for 1 sec after a click

I found following solution but above code works "without onClick(View v)"method and without implementing View.OnClickListener in class. How to provide delay in such case..How code is working without onClick method.

   @Override
    public void onClick(View v) {
        ((Button) findViewById(R.id.click)).setEnabled(false);

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                ((Button) findViewById(R.id.click))
                    .setEnabled(true);

            }
        }, 1000);

    }
});
G.ONE
  • 507
  • 1
  • 5
  • 14
  • did you check following link https://stackoverflow.com/questions/62064123/kotlin-coroutines-delay-how-does-it-work – Jack Philips Jul 30 '21 at 06:36
  • 1
    What you need is some kind of SingleClickListener with an subsequent click interval . [Here it is](https://stackoverflow.com/questions/5608720/android-preventing-double-click-on-a-button). – ADM Jul 30 '21 at 06:38

4 Answers4

1

Use this method when you want to interrupt user clicks:

private static long mLastClickTime = 0L;

public static boolean isOpenRecently() {
    if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
        return true;
    }
    mLastClickTime = SystemClock.elapsedRealtime();
    return false;
}

Usage:

if (v.getId() == R.id.sendBtn) {
        if (isOpenRecently()) return;
        // Your logic
    }
1

Basically you are using lambda in your code, where v->{} represents the onCLick(View v) function.

sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));

You can do the following to disable the button for 1 second

 void doOnSendButtonClick(View v){
    //Send the message (your logic here)
    send(sendText.getText().toString());

    //Disable button
    sendBtn.setEnabled(false);

    //enable button after 1000 millisecond
    new Handler(Looper.getMainLooper()).postDelayed(() -> {
        sendBtn.setEnabled(true);
    }, 1000);
}

And call this method when user clicks on the button

sendButton.setOnClickListener(view -> doOnSendButtonClick(view));
sajalsd
  • 349
  • 1
  • 5
0

You could use a CountDownTimer.

CountDownTimewr timer = new CountDownTimer(1000, 1000) {

    public void onTick(long millisUntilFinished) {
    }

    public void onFinish() {
        // enable button
        ((Button) findViewById(R.id.click)).setEnabled(true);
    }
}

@Override
public void onClick(View v) {
        // disable btn and start timer of one second in millis
        ((Button) findViewById(R.id.click)).setEnabled(false);
        timer.start();
    }
});
Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
0

Please try this way

  • Handler(Looper.getmainLooper()) and remove callback after you done your task inside runnable thread...also try to put this logic in any method outside in this class and call from setonclicklistener

  • Try the Timer.shedule with timertask

  • you can use runOnUIThread method inside where you can update UI.

  • you can also try to check the time difference of click and enable again in looping like while.

gpuser
  • 1,143
  • 1
  • 9
  • 6