5

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.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
craig-nerd
  • 718
  • 1
  • 12
  • 32
  • They don't get disabled as immediately after checking the answer your enable them again. You can use Handler and postDelayed to enabled them after a few sec – fbwnd Jul 20 '17 at 12:04
  • What do you want to actually do in your code? Do you want do disable both button after first click or want to enable after some time? if you want to enable after some time then use handler with post delay method to renable it. – Mohd Saquib Jul 20 '17 at 12:07
  • https://stackoverflow.com/questions/15615823/setenabled-vs-setclickable-what-is-the-difference – Pushan Gupta Jul 20 '17 at 12:11

5 Answers5

6

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);
Ajil O.
  • 6,562
  • 5
  • 40
  • 72
4

Use

mTrueButton.setClickable(false);

instead of

mTrueButton.setEnable(false);
6155031
  • 4,171
  • 6
  • 27
  • 56
1

Try this

mTrueButton.setVisibility(View.INVISIBLE);

mTrueButton.setVisibility(View.VISIBLE);
Ojas Chimane
  • 104
  • 10
1
private static final ScheduledExecutorService worker =
            Executors.newSingleThreadScheduledExecutor();


  checkAnswer(false);
  Runnable task = new Runnable() {
            public void run() {
        mTrueButton.setEnabled(true);
        mFalseButton.setEnabled(true);
  mFalseButton.setEnabled(true);
            }
        };
        worker.schedule(task, 3000, TimeUnit.MILLISECONDS);
Zeeshan Sardar
  • 235
  • 3
  • 15
1

try this

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

        checkAnswer(false); //Display the appropriate Toast


        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
            // This method will be executed once the timer is over
                mTrueButton.setEnabled(true);
                mFalseButton.setEnabled(true);

            }
        },2000);// set time as per your requirement in millisecinds
    }
});
Ajil O.
  • 6,562
  • 5
  • 40
  • 72
AskNilesh
  • 67,701
  • 16
  • 123
  • 163