TransWikia.com

Android Preventing Double Click On A Layout with handher

Stack Overflow Asked by user13380366 on January 12, 2021

There is a button layout that also has a progress bar inside. When the button is clicked, it pauses for a few seconds and enters the next activity.
To prevent double click i think i have to put these codes on OnclickListener

private long mLastClickTime = 0;

if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
    return;
}
mLastClickTime = SystemClock.elapsedRealtime();

But i don’t no where

My onClickListener method:

btnContinue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        progress.setVisibility(View.VISIBLE);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
                    return;
                }
                mLastClickTime = SystemClock.elapsedRealtime();

                progress.setVisibility(View.GONE);
                Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
                startActivity(intent);

            }
        }, 4500);

    }
});

I change my listener to :

btnContinue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        progress.setVisibility(View.VISIBLE);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
                    return;
                }

                progress.setVisibility(View.GONE);
                Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
                startActivity(intent);

            }
        }, 4500);
        mLastClickTime = SystemClock.elapsedRealtime();
    }
});

But when I clicked on the button several times, it opened the next activity several times

One Answer

Please remove handler and put your logic code there e.g.

btnContinue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progress.setVisibility(View.VISIBLE);
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
                        return;
                    }            

                    progress.setVisibility(View.GONE);
                    Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
                    startActivity(intent);

                }
            }, 4500);
          mLastClickTime = SystemClock.elapsedRealtime();
        }
    });

Answered by Vishal Sojitra on January 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP