Search code examples
javaandroidbuttonautomationdelay

Automated button clicking happens simultaneously


I've tried a hundred different methods to implement a delay between automated button clicks, including thread.sleep, Handler.postDelayed, and so on… It could be I have used it incorrectly somehow. My most recent attempt was with a simple boolean toggle. It seems that no matter how I try, all the buttons that are to be automatically clicked happen at the same time after the delay, INSTEAD of being delayed between clicks. my code as it stands now:

setting up button onClickListener:

    for (int i = 0; i < mDifficulty; i++) {
         ButtonsOCLArray[i].setOnClickListener(new OnClickListener() {

            public void onClick(final View v) {
                animating = true;
                while (animating) {
                animateButtons(v);
                }
            }
        });
    }

animation of buttons:

    public static void animateButtons(View v) {
      AlphaAnimation fadeInAnimation = new AlphaAnimation(0F, 1F);
      fadeInAnimation.setDuration(1500);
      fadeInAnimation.setFillAfter(true);
      v.startAnimation(fadeInAnimation);
      animating = false;
    }

and finally, from a separate class, the automatic button setup:

    public void pushAiButton(final View [] v){
       mWhichButton = (mGameAi.getRandomNumber(MainActivity.mDifficulty)); // get random number for random button to press
       mListOfAiButtonsToPress.add(mWhichButton); // add random number to mLOABTP
       mListOfAiButtonsTemp.addAll(mListOfAiButtonsToPress); // add all elements of mLOABTP to mLOABT
       boolean empty = false;
       while (!empty) { 
         if (empty) {
            break;
         }
         tempArrayIndex = mListOfAiButtonsTemp.get(0); // tempArray given value in first slot of mLOABT
         mListOfAiButtonsTemp.remove(mListOfAiButtonsTemp.get(0)); // first slot of MLOABT removed
         if (mListOfAiButtonsTemp.isEmpty()) {
            // looped through whole list, empty now
            empty = true; 
         } // end if
         v[tempArrayIndex].performClick(); // click button at index *button*[*index*]
       } // end !empty while
     } // end pushAiButton()

any ideas HIGHLY appreciated! thanks!

UPDATE This got it working:

    mButtonStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {
            Log.d(TAG, "START BUTTON CLICKED!");
            if (firstRun) {
                mGameAi.setupAiButtons();   
                firstRun = false;
            }
            v.setVisibility(View.INVISIBLE);
            animateButtons(ButtonsOCLArray[mGameAi.getFirstButtonInList()]);
            mGameAi.deleteFirstButtonInList();
            v.postDelayed(new Runnable() {
                public void run() {
                    if (!mGameAi.buttonsListIsEmpty()) {
                        v.performClick();
                    }
                    else {
                        v.setVisibility(View.VISIBLE);
                        firstRun = true;
                    }
                }
            }, 500);
            System.out.println("end of mButtonStart's onclicklistener");
        }
    });

Solution

  • You have coded it so that they all click nearly simultaneously. The automatic button setup does a "while" loop that iterates through all the buttons - removing them one at a time nearly simultaneously.

    In other words, your while loop iterating through the buttons needs to pause (or not queue another click) until the animation is complete.

    Here is the problem said another way; when each "onClick" occurs, the boolean "animateButtons" is true and they all enter into the animateButtons method.

    You need to have a thread with a wait call on in pushAiButton and wait for each button to finish its animation.