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?
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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.