So I have a quiz app, in which I present the user with a question and four options. The 4 options are given in a RecyclerView
, so I am passing the 4 options(as string ArrayList
) and the correct answer(string) to the RecyclerView
adapter constructor.
Now if the chosen answer is correct the itemView
is set to green and if wrong it is set to red(up to this is working fine).
My problem is that
when I press the wrong answer I have to set the background color of the itemview(already drawn) with correct answer to turn green along with the selected wrong answer turning red
See my code below
@Override
public void onBindViewHolder(final OptionsAdapter.ViewHolder viewHolder, final int i) {
viewHolder.tv_name.setText(option.get(i));
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (option.get(i).equals(correct)){
Toast.makeText(context,"CORRECT ANSWER :)",Toast.LENGTH_SHORT).show();
viewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.correctAnswer));
} else {
Toast.makeText(context,"SORRY INCORRECT ANSWER :(",Toast.LENGTH_SHORT).show();
viewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.wrongAnswer));
}
}
});
}
In the above snippet of my onBindViewHolder
, the arraylist option
is having the 4 options and the string correct
is the correct answer.
How can i turn the correct answer to turn green when the wrong answer clicked?
Below image shows what iam expecting when wrong answer clicked