Vivek Annamlai
Vivek Annamlai

Reputation: 53

Disable button after a click for 2 seconds and resume back

When I click a resend button first time, the button will disable for 2 seconds.After 2 seconds the button will enable? I am using this code

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btn.setEnabled(false);
            btn.postDelayed(new Runnable() {
                public void run() {
                    btn.setEnabled(true);
                    Log.d(TAG,"resend1");
                }
            },1000);
        }
    });

But this code is not working properly.

Upvotes: 5

Views: 10119

Answers (4)

Rahul Sharma
Rahul Sharma

Reputation: 87

As of now, we need to pass Looper inside the Handler(), Below is the solution with kotlin, and to keep the application away from UI hanging or ANRs, I have used Coroutines on Main Dispatcher:

binding.buttonId.setOnClickListener(object:View.OnClickListener{
        override fun onClick(p0: View?) {
            
            binding.buttonId.isEnabled=false
            binding.buttonId.isClickable=false
            
            CoroutineScope(Dispatchers.Main).launch {
                Handler(Looper.getMainLooper()).postDelayed(object:java.lang.Runnable{
                    override fun run() {
                        binding.buttonId.isEnabled=true
                        binding.buttonId.isClickable=true
                    }

                }, 2000)
            }
        }

Upvotes: 1

Rajasekhar
Rajasekhar

Reputation: 2345

Find the solution

  1. In your button click

     long  mLastClickTime;
     yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            disableButtonTwoSecs();
          // Here your implementation
    
        }
    });
    
    public static boolean disableButtonTwoSecs() {
    if (SystemClock.elapsedRealtime() - mLastClickTime < 2000) {
        return true;
    }
    mLastClickTime = SystemClock.elapsedRealtime();
    return false;
    }
    

Upvotes: 1

Avinash Roy
Avinash Roy

Reputation: 973

You can use a timer for this

   btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               myButton.setEnabled(false);

    Timer buttonTimer = new Timer();
    buttonTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    myButton.setEnabled(true);
                }
            });
        }
    }, 5000);
            }
        });

Upvotes: 3

AskNilesh
AskNilesh

Reputation: 69681

try this for this purpose you can use Handler(import android.os.Handler;)

  btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        btn.setEnabled(false);

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

        @Override
        public void run() {
            // This method will be executed once the timer is over
                btn.setEnabled(true);
                Log.d(TAG,"resend1");

        }
    },2000);// set time as per your requirement 
    }
});

Upvotes: 13

Related Questions