Reputation: 1051
I want to change the color of a textView in the onBindViewHolder in the Adapter depending on a variable of data. I've tried this
if (survey.getAnonymous() == 0) {
holder.surveyIcon.setImageResource(R.drawable.ic_x);
holder.surveyAnonymous.setTextColor(R.color.red);
} else {
holder.surveyIcon.setImageResource(R.drawable.ic_check);
holder.surveyAnonymous.setTextColor(R.color.green);
}
R.color.red
is underlined in red and says
Should use getResources().getColor(R.color.red)
I tried that and it also gives me an error. I have tried these:
holder.surveyAnonymous.setTextColor(R.color.red);
holder.surveyAnonymous.setTextColor(getResources().getColor(R.color.red));
holder.surveyAnonymous.setTextColor(getColor(R.color.red));
holder.surveyAnonymous.setTextColor(Color.parseColor(R.color.red);
None of these have worked and are always underlined in red, what is the correct way to do this?
Upvotes: 3
Views: 750
Reputation: 75788
int getColor (Context context, int id)
holder.surveyAnonymous.setTextColor(ContextCompat.getColor(contextObject,(R.color.red)));
FYI
Color.parseColor
- Parse the color string, and return the corresponding color-int.
holder.surveyAnonymous.setTextColor(Color.parseColor("#54D66A");
Upvotes: 3