Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable button when clicked, then re-enable it

I am starting out Android development by building a simple quiz app with only two buttons, True and False. The app displays a Toast saying whether the answer is correct or incorrect.
What I am trying to do is disable both the buttons as soon as either is clicked/tapped, then re-enable them after the Toast has been displayed. This is what I tried in my listener for the buttons:

mFalseButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            mTrueButton.setEnabled(false);
            mFalseButton.setEnabled(false);

            checkAnswer(false); //Display the appropriate Toast

            mTrueButton.setEnabled(true);
            mFalseButton.setEnabled(true);
        }
    });

This does not work. My buttons never get disabled when they are clicked/tapped. How can I do this correctly? Any help would be highly appreciated.

like image 953
craig-nerd Avatar asked Jul 20 '17 12:07

craig-nerd


1 Answers

Enable with a Handler like this after 1500 ms.

mTrueButton.setEnabled(false);
mFalseButton.setEnabled(false);

checkAnswer(false); //Display the appropriate Toast

new Handler().postDelayed(new Runnable() {
  @Override
  public void run() {
      mTrueButton.setEnabled(true);
      mFalseButton.setEnabled(true);
  }
}, 1500);
like image 177
Ajil O. Avatar answered Oct 13 '22 07:10

Ajil O.