0

I would like to know how to make this button green when it's clicked if it holds the correct answer. I don't know which button holds the correct answer because its randomly generated. Also I've put in the code that when the button is clicked that it kind of sleeps for a second and then moves to a new a question so I would like that the button turns green for the time that the activity holds so when a new question is generated it turns back in how it was.

public class Glavno extends Activity implements OnClickListener {

  final Handler handler = new Handler();

  int score  = 0;
  int counter = 0;
  boolean ajme = true;

  TextView textView1, textView2, textView3, countdown;
  Button btn1, btn2, btn3, btn4;

  ArrayList<Question> qsts = new ArrayList<Question>();
  List<Integer> generated = new ArrayList<Integer>();

  ArrayList<String> allAnswers = new ArrayList<String>();

  Random rng = new Random();
  Question nextQuestion;

  Question qjedan = new Question(
    "Q1",

    "Correct answer - q1",
    "Wrong answer 3 - q1",
    "Wrong answer 3 - q1",
    "Wrong answer 3 - q1"
  );
  Question q2 = new Question(
    "Q2",

    "Correct answer - q2",
    "Wrong answer 3 - q2",
    "Wrong answer 3 - q2",
    "Wrong answer 3 - q2"
  );
  Question q3 = new Question(
    "Q3",

    "Correct answer - q3",
    "Wrong answer 3 - q3",
    "Wrong answer 3 - q3",
    "Wrong answer 3 - q3"
  );


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.pitanja);

    new CountDownTimer(20000, 1000) {

     public void onTick(long millisUntilFinished) {
       textView4.setText("Seconds remaining: " + millisUntilFinished / 1000);
     }

   public void onFinish() {
     generateQuestion();
     textView2.setText("VRIJEME!");
     textView2.setTextColor(Color.RED);

 }
  }.start();

qsts.add(qjedan);           
qsts.add(q2);
qsts.add(q3);

textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);

textView3.setText("Rezultat: " + score);

btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);

generateQuestion();

}

 public void generateQuestion(){

while(ajme = true){

    int nxt = rng.nextInt(3);

    if (!generated.contains(nxt)){

        generated.add(nxt);

        nextQuestion = qsts.get(nxt);

        textView1.setText(nextQuestion.questionText);

        allAnswers.add(nextQuestion.correctAnswerText);
        allAnswers.add(nextQuestion.wrongAnswer1);
        allAnswers.add(nextQuestion.wrongAnswer2);
        allAnswers.add(nextQuestion.wrongAnswer3);

        Collections.shuffle(allAnswers);

        btn1.setText(allAnswers.get(0));
        btn2.setText(allAnswers.get(1));
        btn3.setText(allAnswers.get(2));
        btn4.setText(allAnswers.get(3));

        break;
    }
}
}


@Override
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();

if (counter == 3) {

    Intent theIntent = new Intent(this, Score.class);
    theIntent.putExtra("somename", score);  
    startActivity(theIntent);

    finish();   // Added this method call
}

else if(buttonText.equals(nextQuestion.correctAnswerText)) { 

    counter++;

    textView3.setText("Rezultat: " + (score += 10));

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

                    allAnswers.clear();
                    generateQuestion();

                }
              }, 1000);

    return;
} 

else{

    counter++;

    textView2.setTextColor(Color.RED);

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

                    allAnswers.clear();
                    generateQuestion();

                }
              }, 1000);

    return; 
}

}   
Jo Ke
  • 145
  • 1
  • 2
  • 12

1 Answers1

0

Use the View that you cast to a Button. You will have to change the declaration to:

final Button b = (Button)v;

Now, if the button holds the correct answer, set its background to green. In your handler, revert the background to the original color:

else if(buttonText.equals(nextQuestion.correctAnswerText)) { 

    counter++;

    textView3.setText("Rezultat: " + (score += 10));

    // Set the background color here
    b.setBackgroundColor(Color.GREEN);

    handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // Revert back to original color
                    b.setBackgroundColor(originalColor);

                    allAnswers.clear();
                    generateQuestion();

                }
              }, 1000);
    return;
} 
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • but how will I know which button holds the correct answer cause its randomly generated – Jo Ke Aug 07 '13 at 22:24
  • @JoKe You are already performing this check: `else if(buttonText.equals(nextQuestion.correctAnswerText))`. If the execution enters this else-if block, it means that the button clicked did hold the right answer. This is where you set the background color. Try it and see if it works. – Vikram Aug 07 '13 at 22:27
  • @JoKe Somehow you have to keep track of which button holds the correct answer and then test for it. – Code-Apprentice Aug 07 '13 at 22:40
  • @MonadNewb Correct me if I'm wrong, but the `else if` condition already takes care of that. – Vikram Aug 07 '13 at 22:43
  • @vikram From what I can tell, I think you are right. I'm just pointing out to the OP that *they* have to take care of this detail in some manner. Java can't just magically pick out which button is "correct". – Code-Apprentice Aug 07 '13 at 22:45
  • @MonadNewb `Java can't just magically pick out which button is "correct"`. :), absolutely. – Vikram Aug 07 '13 at 23:03
  • @vikram Glad you know it ;-) I just hope the OP read these comments since they are the intended audience even though I am pinging you =p – Code-Apprentice Aug 07 '13 at 23:16