There is a TextView in which you need to select in a different color all the letters to the first comma. I want to highlight the appeal to the nickname in the chat.

Now there are two TextView , but the solution is crutch and does not look very good, as the text of the message is on the right, and you need a flow around the message.

All the same, we need a way to color the text in one TextView . How? I don’t even know, I can’t give the attempt code, as it’s elementary, I don’t know how to implement it

UPD

I heard about html codes, but I don’t understand how to use it if the length of the message is unknown (before the first comma)

1 answer 1

  TextView TV = (TextView)findViewById(R.id.mytextview01); Spannable word = new SpannableString("Your message"); word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TV.setText(word); Spannable wordTwo = new SpannableString("Your new message"); wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TV.append(wordTwo); 

It is possible through html for example

 <string name="signal_msg3"> <![CDATA[ Добавьте номера Ваших близких в <font color=\'#cc0000\'>Экстренные контакты</font> ]]> </string> 

And TV.setText(Html.fromHtml(getString(R.string.signal_msg3)));