Abhishek Jha
Abhishek Jha

Reputation: 167

multiple background color to recyclerview item's TextView at n intervals in android

I need to make my RecyclerView item's TextView display multiple background color. Let's say i have 7 different color codes that i need to show after every 7 items. Here's my approach towards it. Please help!!

@Override
public void onBindViewHolder(BuyCategoriesViewHolder holder, final int position) {
    holder.tv_name.setText(category.get(position).getCategory());
    char firstChar=category.get(position).getCategory().charAt(0);
    holder.tv_title.setText(String.valueOf(firstChar));

    if(position == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
    }else if(position %7 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color2));
    }else if(position %6 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color3));
    }else if(position %5 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color4));
    }else if(position %4 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color5));
    }else if(position %3 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color6));
    }else if(position %2 == 0){
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color7));
    }else {
        holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
    }

    //holder.tv_title.setBackgroundResource(R.drawable.shape_circle);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            callItemViewListener.callItemView(position);
        }
    });

}

Upvotes: 4

Views: 7043

Answers (3)

RobCo
RobCo

Reputation: 6495

You are using the modulus operator in the wrong order. Try it like this:

if (position % 7 == 0) {
    holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color1));
} else if (position % 7 == 1) {
    holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color2));
} else if (position % 7 == 2) {
    holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color3));
} else if (position % 7 == 3) {
    holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color4));
} else if (position % 7 == 4) {
    holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color5));
} else if (position % 7 == 5) {
    holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color6));
} else if (position % 7 == 6) {
    holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext,R.color.list_color7));
}

Or with a switch statement:

int colorRes = 0;
switch(position % 7) {
    case 0: colorRes = R.color.list_color1;
        break;
    case 1: colorRes = R.color.list_color2;
        break;
    case 2: colorRes = R.color.list_color3;
        break;
    case 3: colorRes = R.color.list_color4;
        break;
    case 4: colorRes = R.color.list_color5;
        break;
    case 5: colorRes = R.color.list_color6;
        break;
    case 6: colorRes = R.color.list_color7;
        break;
}
holder.tv_title.setBackgroundColor(ContextCompat.getColor(mContext, colorRes));

Edit
For completeness, incorporate @iClaude 's answer with an example.
This defines an array of color resources and then uses the modulus operator to get the correct index for the array:

// first define colors  
private final int[] backgroundColors = {
    R.color.list_color1, 
    R.color.list_color2, 
    R.color.list_color3,
    R.color.list_color4, 
    R.color.list_color5, 
    R.color.list_color6, 
    R.color.list_color7 };


// in onBindViewHolder
int index = position % backgroundColors.length;
int color = ContextCompat.getColor(mContext, backgroundColors[index]);
holder.tv_title.setBackgroundColor(color);

Upvotes: 18

Prabh deep
Prabh deep

Reputation: 1042

Use This inside of Adapter Class

  String[] mColors = {"#3F51B5", "#FF9800", "#009688", "#673AB7", "#999999", "#454545", "#00FF00",
                    "#FF0000", "#0000FF", "#800000", "#808000", "#00FF00", "#008000", "#00FFFF",
                    "#000080", "#800080", "#f40059", "#0080b8", "#350040", "#650040", "#750040",
                    "#45ddc0", "#dea42d", "#b83800", "#dd0244", "#c90000", "#465400",
                    "#ff004d", "#ff6700", "#5d6eff", "#3955ff", "#0a24ff", "#004380", "#6b2e53",
                    "#a5c996", "#f94fad", "#ff85bc", "#ff906b", "#b6bc68", "#296139"};
            productViewHolder.brandname.setBackgroundColor(Color.parseColor(mColors[i % 40]));

Upvotes: 5

iClaude
iClaude

Reputation: 163

Create an array (0 to 6) with your colors, then use position % 7 to get the offset in the array (the color). By doing so you also get rid of that ugly if..else construct with many branches.

Upvotes: 1

Related Questions