-2

I have one problem to show text with different color word then I search it and find this page

Change text color of one word in a TextView

and this page

Different font size of strings in the same TextView

and this page

How to load external webpage inside WebView

there answer does not solve my problem because I have very long text to colored it and also I want text like that image

enter image description here

and this code for one word like image not work

final SpannableStringBuilder ssb = new SpannableStringBuilder();
final int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;

//converting arabic text to unicode chars
String dd= "بِسْمِ" ;
ssb.append(dd);
//applying colors
ssb.setSpan(new ForegroundColorSpan(Color.RED), 1, 2, flag);
ssb.setSpan(new ForegroundColorSpan(Color.RED), 3, 4, flag);
ssb.setSpan(new ForegroundColorSpan(Color.RED), 5, 6, flag);
//seting formated spanned text
TextView mTextView=(TextView) findViewById(R.id.textView);
  mTextView.setText(ssb);

Please answer this question with simple code

1 Answers1

1

I would recommend you to use Spannable:

SpannableStringBuilder mySpanText= new SpannableStringBuilder("Some long sentence");

Define text color for a piece of string(i.e. the word "long"):

mySpanText.setSpan(new ForegroundColorSpan(Color.RED),
                0, mySpanText.length(), 0 );

Assign the text value to your TextView:

TextView myTextView =(TextView) findViewById(R.id.name_tv);

myTextView .setText(mySpanText, BufferType.SPANNABLE);

For more ways and for documentation, look in the links below:

Cody Gray
  • 222,280
  • 47
  • 466
  • 543
HaroldSer
  • 1,724
  • 2
  • 9
  • 17
  • Hi Scott! Please do not use blockquote formatting for things that are not actually quotations. Plain text works just fine here, since the code blocks provide enough separation. If you need separation between blocks of text, you might use a horizontal line (`----`). – Cody Gray Aug 12 '17 at 14:01
  • Thanks for the suggestion Cody. – HaroldSer Aug 12 '17 at 16:34
  • thanks for Guide me ///// I am searching and found a lot of tutorial about Spannable – mahdi porkazeme Aug 13 '17 at 06:44