Akkuu
Akkuu

Reputation: 1

How to trigger a Button Click if a button is not being clicked by the user?

I'm creating a tapping game, in which the player will tap in the buttons and will earn points. I used random fucntion inside the button onclick to move the button randomly.

I want the button to be triggered again, only if the player is not clicking on it (after some elapsed time), so that it will appear randomly once again.

Upvotes: 0

Views: 58

Answers (2)

Adonys
Adonys

Reputation: 123

try this, it start after user click at least once time the button

    final Handler h = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //function moveButton
        }
    };


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   //....

    final long timeToMoveButton = 30000; //milliseconds

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            h.removeCallbacks(runnable);
            h.postDelayed(runnable, timeToMoveButton);
        }
    });
}

if you want move button when app start add in onCreate()

h.postDelayed(runnable, timeToMoveButton);

Upvotes: 0

Bret Deasy
Bret Deasy

Reputation: 830

I think you can use buttonObject.performClick()

Upvotes: 2

Related Questions