0

I want to set color just on "read more" string :

    holder.Title.setText(current.getTitle());
    holder.Description.setText(start+"...."+"read more");
    holder.Date.setText(current.getPubDate());

I have tried to use html.fromhtml but it is not working with me !!!

3 Answers3

2

Try as follow

String textFirstPart = start + "....";
String textSecondPart = "read more";
String text = textFirstPart + textSecondPart;
Spannable spannable = new SpannableString(text);
spannable.setSpan(new ForegroundColorSpan(Color.RED), textFirstPart.length(),
                            (textFirstPart + textSecondPart).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.Description.setText(spannable, TextView.BufferType.SPANNABLE);
Abner Escócio
  • 2,697
  • 2
  • 17
  • 36
1

Starting from Android N,

the method Html.fromHtml(htmlText) is deprecated and you have to use

Html.fromHtml(htmlText, MODE) instead, so use the following condition,

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
           holder.setText(Html.fromHtml(sourceString,Html.FROM_HTML_MODE_LEGACY);
        } else {
           holder.setText(Html.fromHtml(sourceString);
        }

Reference: https://developer.android.com/reference/android/text/Html#FROM_HTML_MODE_COMPACT

MRah
  • 940
  • 6
  • 15
1

Here you go

 SpannableString styledString = new SpannableString("read more");
 // change text color
 styledString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 8, 0);
 // underline text
 styledString.setSpan(new UnderlineSpan(), 0, 8, 0);

Read more here

vikas kumar
  • 10,447
  • 2
  • 46
  • 52