0

I'm developing a quiz app. I want to change the button colour when the user selects it and then change it when the option is right or wrong. But the button color changes to grey even though I've not defined this color anywhere. How to fix this?

Here's my code:

 private void highlightCorrectAnswer() { if (currentQuestion.getOptionR().equals(currentQuestion.getOptionA())) { optionA.setBackgroundColor(ContextCompat.getColor(MockQuizActivity.this, R.color.Success)); } else if (currentQuestion.getOptionR().equals(currentQuestion.getOptionB())) { optionB.setBackgroundColor(ContextCompat.getColor(MockQuizActivity.this, R.color.Success)); } else if (currentQuestion.getOptionR().equals(currentQuestion.getOptionC())) { optionC.setBackgroundColor(ContextCompat.getColor(MockQuizActivity.this, R.color.Success)); } else if (currentQuestion.getOptionR().equals(currentQuestion.getOptionD())) { optionD.setBackgroundColor(ContextCompat.getColor(MockQuizActivity.this, R.color.Success)); } } 
 private void resetButtonState() { optionA.setEnabled(true); optionB.setEnabled(true); optionC.setEnabled(true); optionD.setEnabled(true); Log.d(TAG, "resetButtonState: "+"resitting button states"); optionA.setBackground(ContextCompat.getDrawable(this, R.drawable.button_outlined)); optionB.setBackground(ContextCompat.getDrawable(this, R.drawable.button_outlined)); optionC.setBackground(ContextCompat.getDrawable(this, R.drawable.button_outlined)); optionD.setBackground(ContextCompat.getDrawable(this, R.drawable.button_outlined)); } 

I've checked the code is working correctly. But the color is not being set to what I've defined. Like R.color.success is not being applied.

3
  • Have you checked with the debugger or with a Log.d() statement that the setBackgroundColor code actually is executed? Have you tried using something like Button11.setBackgroundColor(Color.RED); instead to find out if the R.color is causing the problem? Please try these things and then edit your post and include the results you found. Commented Jul 22, 2024 at 8:11
  • Yes. I've tried those. They don't work. And yes this code is actually being implemented. I checked with log statements. Commented Jul 22, 2024 at 8:17
  • Please try using backgroundtintlist in this instead of backgroundcolor maybe it will solve your problem. Commented Jul 22, 2024 at 12:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.