1

can any 1 give me example to perform onClick on button for multiple times when user clicked for 1 time. when i click on button 1 time it should automatically click after delay of 5 seconds for 100 times. how to perform that. This is my sample code

mUnlock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //It should be already ensured that this mSelectedLock is something user is authorized to access
                    if (mSelectedLock.unlock("RANDOM")) {
                        mUnlock.setVisibility(View.INVISIBLE);
                        mUnlock.postDelayed(new Runnable() {
                            public void run() {
                                mUnlock.setVisibility(View.VISIBLE);

                            }
                        }, 5000);


                    } else {
                        Toast.makeText(MainActivity.this, "Unable to unlock.", Toast.LENGTH_LONG).show();
                    }
                }
            });
4
  • use Handle for delay and disable button click Commented Dec 11, 2015 at 9:50
  • buttton.performClick(); Commented Dec 11, 2015 at 9:51
  • i can disable button. but i need to perform onclick operation for 100 times Commented Dec 11, 2015 at 9:51
  • you cant access to UI element in another thread .... Commented Dec 12, 2015 at 7:14

2 Answers 2

1

@Override public void onClick(View v) {actionToBeDone();startLoop(0);}

private void startLoop(final int i) {
    if(i!=100) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e("i",""+i);
                actionToBeDone();
                startLoop(i+1);
            }
        }, 2000);
    }
}

private void actionToBeDone() {
    //enter actions you want to be done
    Log.e("actionToBeDone","Button Action");
}
Sign up to request clarification or add additional context in comments.

2 Comments

call these 2 functions in your onclick
make it 5000 instead of 2000
0
int count = 0;    
Timer timer = new Timer();
timer.schedule(new TimerTask(){
   @Override
   public void run(){
      if(count < 100){
          mUnlock.performClick();
      }
   }
}, 0, 5000);

5000 is the time in miliseconds you can +/- from here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.