Michael Joseph
Michael Joseph

Reputation: 47

How to set color on string

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 !!!

Upvotes: 0

Views: 337

Answers (3)

MRah
MRah

Reputation: 950

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

Upvotes: 1

vikas kumar
vikas kumar

Reputation: 11028

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

Upvotes: 1

Abner Escócio
Abner Escócio

Reputation: 2785

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);

Upvotes: 2

Related Questions