I am fetching data from server through retrofit. If the data is positive the color should be green else it should be red. Just like the Sensex if it is positive then it shows green color else red. See Image for reference.Onbind viewholder i done below but it is for position in grid view
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if(position % 2 ==0) {
holder.itemView.setBackgroundColor(
ContextCompat.getColor(holder.itemView.getContext(), R.color.red));
} else {
holder.itemView.setBackgroundColor(
ContextCompat.getColor(holder.itemView.getContext(), R.color.green));
}
How to achieve that.
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
val data = list[holder.adapterPosition]
if (data.changeValue > 0) {
holder.itemView.setBackgroundColor(
ContextCompat.getColor(holder.itemView.getContext(), R.color.red));
} else {
holder.itemView.setBackgroundColor(
ContextCompat.getColor(holder.itemView.getContext(), R.color.green));
}
}
where data.changeValue
is the data value coming from the API like 207.57 or -63.85 (I just assumed). I hope this solution will solve your problem.