1

I have a string which I have to show using a TextView. Let's say that the string is :

String stringToBeShown = "Baby wants to cry".

The resultant using a TextView that I want is :

enter image description here

I have tried some HTML tricks but couldn't make it work. Like :

String stringToBeShown = "Baby wants to <font color='#EE0000'>cry</font>".  
t.setText(Html.fromHtml(stringToBeShown));

I want to give some background to each word except the spaces in that string. Is there anything that could make it achievable.

BST Kaal
  • 2,993
  • 6
  • 34
  • 52
  • "I have tried some HTML tricks but couldn't make it work." : Post the code of what you've tried. – Squonk Sep 19 '15 at 21:22
  • 2
    Yes, you can achieve this. Here is an example: [background colors for each word](http://stackoverflow.com/questions/26092216/android-textview-with-different-background-colors) – cylon Sep 19 '15 at 21:23
  • @Squonk, besides the tag, I have also tried using but that's in vain too. – BST Kaal Sep 19 '15 at 21:28
  • @cylon, For that purpose, I have to calculate each word indexes, isn't that? – BST Kaal Sep 19 '15 at 21:30
  • Yes, exactly. Did it help? – cylon Sep 19 '15 at 21:39
  • 1
    @BSTKaal : The answer in the post linked to by cylon should work well. You could simply split the original string at each space to get an array of words then use each word's length to set the background of each word in the SpannableString. – Squonk Sep 20 '15 at 09:42
  • @Squonk, i have tried the solution given in that link and getting each word backgrounded separately as i wanted. But there is a problem, if a TextView's text goes to next line, the background of upper line word and the background of lower line words are getting mixed up. I have tried using linespacing attribute of textview but couldn't solve that. Is there any work arround for that? – BST Kaal Sep 22 '15 at 21:42

0 Answers0