I have a problem with setting color from resources for an item in RecyclerView
.
I've tried this two methods but none works. Any ideas what am I doing wrong?
holder.alert.setTextColor(R.color.alertGreen);
holder.alert.setTextColor(getResources().getColor(R.color.alertGreen));
Use ContextCompat
to get color.
holder.alert.setTextColor(ContextCompat.getColor(context, R.color.alertGreen));
To update the color for the Single item you can follow below technique,
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Green color to set to specific item in the view [By referencing the position you need to handle the view]
int color1 = ContextCompat.getColor(context, R.color.alertGreen));
// Red color to set to remaining Views
int color2 = ContextCompat.getColor(context, R.color.alertRed));
if (position == 1) {
holder.alert.setTextColor(color1);
} else {
holder.alert.setTextColor(color2);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With