0

BackgroundColorSpan is not working in recyclerView. It is working properly with the listView but not with recyclerView. Any idea why? or How can i overcome through this problem.

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.no.setText(String.valueOf(position + 1));
        ExerciseRoutineService exerciseRoutineService = 
        exerciselist.get(position);
        String exerciseText = "i exercise by " + 
                                  exerciseRoutineService.getExerciseType()
                                  + " for about " + 
                                  exerciseRoutineService.getFrequency() + 
                                  " " + 
                                  exerciseRoutineService.getDuration() + " 
                                  minutes";
        holder.exercise.setText(exerciseText);
        int start;
        start=exerciseText.indexOf
        (exerciseRoutineService.getExerciseType());
        int end = start + 
        exerciseRoutineService.getExerciseType().length();
        StringBuilder captionBuilder = new StringBuilder();
        captionBuilder.append(exerciseText);
        SpannableStringBuilder commentBuilder = new 
        SpannableStringBuilder(captionBuilder);
        commentBuilder.setSpan(new BackgroundColorSpan(Color.GREEN), 
        start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.exercise.setText(commentBuilder);
 }
Aju
  • 4,597
  • 7
  • 35
  • 58

1 Answers1

0

Changes the background color of the text to which the span is attached. For example, to set a green background color for a text you would create a SpannableString based on the text and set the span.

SpannableString string = new SpannableString("Text with a background color span");
string.setSpan(new BackgroundColorSpan(color),12,28,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Or please see this SpannableStringBuilder and Developer.android.com

Raifur-rahim
  • 543
  • 1
  • 3
  • 12