-1

This may be a ridiculous question ever, but still I want to reduce my effort of creating a lot of text views.

I have text view that contains Name:Value format [Suppose Name:Android] In this case all the attributes for the text view will be same except the color and also the texts are side by side.

In real implementation I have to create two text views, and suppose if I have around 10-15 such pairs, the number of text views will be 20-30 respectively.

So how can I set different color for name and value independently??

Jonny C
  • 1,943
  • 3
  • 20
  • 36
DJphy
  • 1,292
  • 1
  • 17
  • 31
  • 1
    With a [`ForegroundColorSpan`](http://developer.android.com/reference/android/text/style/ForegroundColorSpan.html). You can probably find examples of how to use it by searching here at StackOverflow. – Michael May 07 '15 at 09:26
  • sound new to me..let me try and come back..Gracious coz thanks not allowed in stack(Lolzz).. – DJphy May 07 '15 at 09:28

2 Answers2

1

Use something like

String str = "<font color=#900000 >Name:</font> <font color=#0000FF>Android</font>";
textview.setText(Html.fromHtml(str));
Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
0

You can set the text to use html tags to set colors inside the text

String formattedText = "<font color=\"#ff0000\">red</font> <font color=\"#00ff00\">green</font>";
Spanned result = Html.fromHtml(formattedText);
view.setText(result);

Or, use spannable like in Set color of TextView span in Android

Community
  • 1
  • 1
grebulon
  • 7,697
  • 5
  • 42
  • 66