0

I would like to know how to set an if else condition where the next button have to be disabled if none of the buttons are being clicked. Otherwise, they are able to proceed to next question?

private AdvancedQuestion nAdvancedQuestion = new AdvancedQuestion();

private TextView nScoresView;
private TextView nQuestionsView;
private TextView tvTime;
private Button nButtonChoices1;
private Button nButtonChoices2;
private Button nButtonChoices3;
private Button nButtonChoices4;


private String nAnswers;
private int nScores = 0;
private int nQuestionNumbers = 0;

Button btnNextz;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_advanced_quiz);


    updateQuestions();

    nButtonChoices1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (nButtonChoices1.getText() == nAnswers) {
                correctSound.start();
                nScores = nScores + 1;
                nButtonChoices1.setEnabled(false);
                nButtonChoices2.setEnabled(false);
                nButtonChoices3.setEnabled(false);
                nButtonChoices4.setEnabled(false);
                nButtonChoices1.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
                Toast.makeText(advancedQuiz.this, "correct", Toast.LENGTH_SHORT).show();

            } else {
                wrongSound.start();
                Toast.makeText(advancedQuiz.this, "wrong", Toast.LENGTH_SHORT).show();
                nButtonChoices1.setEnabled(false);
                nButtonChoices2.setEnabled(false);
                nButtonChoices3.setEnabled(false);
                nButtonChoices4.setEnabled(false);

            }
        }
    });

    btnNextz.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nextSound.start();
            updateQuestions();
            nButtonChoices1.setEnabled(true);
            nButtonChoices2.setEnabled(true);
            nButtonChoices3.setEnabled(true);
            nButtonChoices4.setEnabled(true);
            nButtonChoices1.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
            nButtonChoices2.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
            nButtonChoices3.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
            nButtonChoices4.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);

}

private void updateQuestions() {
    nQuestionsView.setText(nAdvancedQuestion.getQuestions(nQuestionNumbers));
    nButtonChoices1.setText(nAdvancedQuestion.getChoices1(nQuestionNumbers));
    nButtonChoices2.setText(nAdvancedQuestion.getChoices2(nQuestionNumbers));
    nButtonChoices3.setText(nAdvancedQuestion.getChoices3(nQuestionNumbers));
    nButtonChoices4.setText(nAdvancedQuestion.getChoices4(nQuestionNumbers));

    nAnswers = nAdvancedQuestion.getCorrectAnswers(nQuestionNumbers);
    nQuestionNumbers++;
}

private void updateScore(int points) {
    nScoresView.setText("" + nScores);
}

Please note there is 4 possible answers. If none of them are selected, they cannot proceed to the next question until one button is press so they can go to the next question. The updateQuestions() is the part where i believe it will show next question.

  • Do you want to check if one of the possible answers is selected and make the "next" button clickable only if yes? – deHaar Aug 04 '17 at 17:17
  • Oh no, as long as none of the button or in other words, any of the 4 possible answers are not selected, they cannot proceed to next question – Beginnercoder Aug 04 '17 at 17:18
  • Well, that is exactly what I meant in the comment before… – deHaar Aug 04 '17 at 17:20
  • Welcome to Stack Overflow, @Beginnercoder. Some more information would be helpful in providing answers. For a start, what have you tried so far? What is not working about that attempt? Perhaps reading this: https://stackoverflow.com/help/mcve will be helpful. – Degan Aug 04 '17 at 17:20
  • Opps, my apologies, i misread your comment. – Beginnercoder Aug 04 '17 at 17:21
  • Let me try show my code above. – Beginnercoder Aug 04 '17 at 17:23

2 Answers2

0

This is a simple example on how to disable/enable a button based on an if condition -

int count = 0;

if (count == 0) {
    NextButton.setEnabled(false);
}
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.otherButtons:
                count++;
                NextButton.setEnabled(true);
                Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show();
                break;


        case R.id.nextButton:
            //Move the user to the next question
            break;        
    }
}

Also check out this link

hsm59
  • 1,991
  • 19
  • 25
  • Alright thank you, i will try it out with that code. – Beginnercoder Aug 04 '17 at 17:19
  • I have made a few changes, check that and let me know if you need any help. – hsm59 Aug 04 '17 at 17:23
  • Alright, i had also posted some parts of my code to make it look more convenient to read since its a very long chunk of code. – Beginnercoder Aug 04 '17 at 17:29
  • But may i know what is with the count? – Beginnercoder Aug 04 '17 at 17:30
  • I'd suggest you to implement the View.OnClickListener to your Activity Class, and separate the onClick logic in it's own method, and use a switch case in order to handle the clicks for each button. – hsm59 Aug 04 '17 at 17:31
  • The count is basically to know whether the user has clicked on a particular button or not, so if the user clicks on one button, the count is increased to 1, and then to enable/disable the NextButton it will check if the count > 0 – hsm59 Aug 04 '17 at 17:33
  • I am sorry, i am really not very good in coding. May i request for you to use my code to write the solution above? It will be more easier for me to understand the theory. – Beginnercoder Aug 04 '17 at 17:34
  • Cool, I will go through it and update my answer accordingly. – hsm59 Aug 04 '17 at 17:35
  • Oh wait, the next button is disabled! However, the only problem is that when i click on one possible answer, the next button is still disabled. Also, i forgot to mention there is 15 questions in total. – Beginnercoder Aug 04 '17 at 17:38
  • You can enable the nextButton once the user has clicked on any one answer, you can apply the same logic for all the 15 questions. – hsm59 Aug 04 '17 at 17:40
  • This part of code ensures that if the user clicks on even one button, the next button is enabled - `case R.id.otherButtons: count++; NextButton.setEnabled(true); Toast.makeText(this, "Button Disabled", Toast.LENGTH_LONG).show(); break;` – hsm59 Aug 04 '17 at 17:43
  • if (count == 0) { btnNextz.setEnabled(false); } else { count++; btnNextz.setEnabled(true); btnNextz.setOnClickListener(new View.OnClickListener() { – Beginnercoder Aug 04 '17 at 17:44
  • No, read the code in the answer, the count++ and btnNext.setEnabled(true) is inside the onClick method of the otherButtons. – hsm59 Aug 04 '17 at 17:45
  • You need to add that part of the code in every nButtonChoices onClick. – hsm59 Aug 04 '17 at 17:48
0

You will need to add a button element in the view of the activity you want it to appear on, then add an event listener to it either in the activity code or specify which function to call on click in the activities XML layout file.

See: https://developer.android.com/reference/android/widget/Button.html