Sagar Duhan
Sagar Duhan

Reputation: 257

Make all buttons un-clickable for 5 seconds

I am trying to make an online tick tac toe game (turn based) in Android Studio.

Is there a way to make all buttons in my app not work for 5 seconds after clicking a button.I intend to make an object containing this function and call it with an onClick function in my java code so that every time I click on a block, All the buttons (blocks) present in that particular activity becomes un-clickable (but still appear on the screen) for a period of 5 seconds and after that they become normal (clickable).

Upvotes: 2

Views: 1842

Answers (4)

CoderP
CoderP

Reputation: 1371

In your onClick() method, you can disable all buttons using setClickable(false). Note that you need to perform this operation individually for all the buttons. For example,If there are two buttons : button1 and button2 that need to be disabled then do

button1.setClickable(false);
button2.setClickable(false);

Once you do for all your buttons, use a Handler to enable them after 5 seconds

final Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
    public void run() {
      enableButtons();
    }
}, 5000);

Likewise, in the enableButtons() method make setClickable(true) for all buttons.

Upvotes: 0

Rashed Zzaman
Rashed Zzaman

Reputation: 123

  1. create a button (name-disable or anything) and implement click listener
  2. put your code like ..

    button1.setEnabled(false);
    button2.setEnabled(false);
    button3.setEnabled(false);
    

after this lines put this method same listener

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

    button1.setEnabled(true);
    button2.setEnabled(true);
    button3.setEnabled(true);
        }
    }, 5000);

Upvotes: 0

Ronak Joshi
Ronak Joshi

Reputation: 1583

You can use Handler and put all buttons there like this :

    final Button btn1 = (Button) findViewById(R.id.btn1);
    final Button btn2 = (Button) findViewById(R.id.btn2);
    final Button btn3 = (Button) findViewById(R.id.btn3);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btn2.setClickable(false);
            btn3.setClickable(false);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    btn2.setClickable(true);
                    btn3.setClickable(true);
                }
            }, 5000);
        }
    });

Upvotes: 2

dario.budimir
dario.budimir

Reputation: 161

After click on button add this

TicTacToeButton.setEnabled(false);
TicTacToeButton.postDelayed(new Runnable() {
    @Override
    public void run() {
        TicTacToeButton.setEnabled(true);
    }
}, 5000);

Upvotes: 0

Related Questions