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.
Log.d()statement that thesetBackgroundColorcode actually is executed? Have you tried using something likeButton11.setBackgroundColor(Color.RED);instead to find out if theR.coloris causing the problem? Please try these things and then edit your post and include the results you found.