0

I hope that the word "Click here" display red color, but the following code don't work, how can I do ? Thanks!

TextView mLink = (TextView) findViewById(R.id.tvMyLink);
Spanned my=android.text.Html.fromHtml(getString(R.string.mylink));   
mLink.setText(my);


<string name="mylink"><span style="color:#FF0000;">Click here </span> for the latest news!</string>

 <TextView
        android:id="@+id/tvMyLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"           
        android:text="@string/mylink" />
HelloCW
  • 843
  • 22
  • 125
  • 310
  • 4
    possible duplicate of [Single TextView with two different colored text](http://stackoverflow.com/questions/6094315/single-textview-with-two-different-colored-text) – M D Feb 25 '15 at 07:43

2 Answers2

0

Try this.

mLink.setText(Html.fromHtml("<font color=\"#FF0000\"> Click here " + "</font>" + "for the latest news!"));
Jignesh Jain
  • 1,518
  • 12
  • 28
0

You can format the String with html's font-color property then pass it to the method

Html.fromHtml(your text here)

String text = "<font color=#cc0029>First Color</font> <font 
color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));

Don't forget to escape userinput using Html.escapeHtml(str)

Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53