-2

I have a TextView in which I have to set a Text. But I'm adding (Concatenating) some text to it and I want only that concatenated text to get some color but not the whole textstoryLine=storyLine.substring(0,190)+" ...Click To Expand";. Here storyLine is a final text that is to be set in a textview. I just want change color of "Click To Expand".

Abhishek Kumar
  • 4,532
  • 5
  • 31
  • 53
  • 1
    Possible duplicate of [Is it possible to have multiple styles inside a TextView?](https://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a-textview) – Jordan Nov 10 '17 at 11:08

4 Answers4

0
String cntStr = " ...Click To Expand";
String storyLine = storyLine.substring(0,190);
String textLine = storyLine + cntStr;

Spannable spannable = new SpannableString(textLine);

spannable.setSpan(new ForegroundColorSpan(Color.RED), storyLine.length(), textLine.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

myTextView.setText(spannable, TextView.BufferType.SPANNABLE);

Happy Coding !!!!

Flutterian
  • 1,761
  • 1
  • 21
  • 47
-1

You can try

String styledText = storyLine + "<font color='blue'>Click To Expand</font>";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
nayana bhoj
  • 115
  • 1
  • 11
-1

You can achieve by this :

SpannableStringBuilder builder = new SpannableStringBuilder();
String red = "Click To Expand";
builder.append("storyLine=storyLine.substring(0,190)+" ...");
SpannableString redSpannable= new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(redSpannable);
mTextView.setText(builder, BufferType.SPANNABLE);

Happy coding!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
-1

You can use SpannableString to color the sub string.

SpannableString span1=new SpannableString("Hello World...Click to Expand");
span1.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 29, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Textview.setText(span1,TextView.BufferType.SPANNABLE);

You can also concatenate

Textview.setText(TextUtils.concat(AnyString, span1));

Set Texview type spannable in Layout xml if needed.

Qasim Ali
  • 66
  • 9