0

I develop a quiz application in Android. I want to show all question on single activity. When user selected a choice, if it is correct, set color is green, if it is false, set color red. And get new question and choices. I wrote code on onClick event:

public void onClickChoice(View v)  {
    String text = ((TextView) v).getText().toString();

    if (text == currentAnswer) {
        v.setBackgroundColor(getResources().getColor(newGreen));
    } else {
        v.setBackgroundColor(getResources().getColor(newRed));
    }

    setNewQuestion();
}

When run application, I answered first question and second question is came, and color of second question's choice is changed. What should I do for solving this problem.

M Hilmi Koca
  • 131
  • 3
  • 16

3 Answers3

0

Why not try with setOnClickLisenter(); implemented in the class

0

Use

 v.setBackgroundResource(R.color.red); //or green, blue,...

Original answer: here

Community
  • 1
  • 1
Curio
  • 1,331
  • 2
  • 14
  • 34
  • @MHilmiKoca From what I understood, you want to see the background changes. Well, if you change it and then you skip to another question, of course the second background is green/red when you are in the background of the other question. So you have to wait. Where's the problem? Can you explain yourself better? – Curio Apr 17 '17 at 21:22
  • I don't see new color (green/red) on first question. Immediately(or 3 second later) second question come with red/green color. My expectation is I answer first question and choice's color is change. And get second question with default color. I answer second question and its choice's color is change. Etc – M Hilmi Koca Apr 17 '17 at 21:37
  • @MHilmiKoca oh so forget the previous code. Use v.setBackgroundResource(R.color.red); – Curio Apr 17 '17 at 21:39
0

Is this in a list or recycler view?

if so, each view is recycled, so you'll need to specifically set the background colour on each view as it initiates.

Matthew Shearer
  • 2,715
  • 3
  • 23
  • 32