1

I am using 2 parts in a textview, 1st part is date another is name and email. They are both referenced in the same textview. I would like to change the color of the date to give it a different visual it from name and email. is it possible to do this without actually adding a whole new textview for name and email? Here's my code so far:

String nameandemail;
holder.mytext.setText(String.valueOf(dateFormat.format(new Date(msg.getDate())) + " " + nameandemail + ": "));

How do I make it such that I can set the color of date with holder.mytext.setTextColor(Color.white) and for the nameandemail string something like green?

Thanks!

3 Answers3

1

You can Use spans.

final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");

// Set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158)); 

// Make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); 

// Set the text color for first 6 characters
sb.setSpan(fcs, 0, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

// make them also bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

textView.setText(sb);

You can also use html like below

  myTextView.setText(Html.fromHtml(text + "<font color=white>" + some_text + "</font><br><br>"
        + some_text));
Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
0

My recommendation would be to use Spannable.

Here is a short utils method I wrapped up for you to use. You simply need to pass your TextView, your full text and the single part to be re-colored from the full text.

You can place this method to a Utils class and call it whenever you want, or keep it in a single Activity or Fragment(or wherever else) if you use it in a single class:

public static void colorText(TextView view, final String fullText, final String whiteText) {
    if (fullText.length() < whiteText.length()) {
        throw new IllegalArgumentException("'fullText' parameter should be longer than 'whiteText' parameter ");
    }
    int start = fullText.indexOf(whiteText);
    if (start == -1) {
        return;
    }
    int end = start + whiteText.length();

    SpannableStringBuilder finalSpan = new SpannableStringBuilder(fullText);
//    finalSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(view.getContext(),R.color.your_own_color_code)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    finalSpan.setSpan(new ForegroundColorSpan(Color.WHITE), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    view.setText(finalSpan);
}
Mike
  • 4,550
  • 4
  • 33
  • 47
  • thats the first thing i tried. It crashes with the error the string extends beloynd limit 30 –  Oct 20 '15 at 21:14
  • But you should not use those numbers. You should adapt to your own needs and use the starting character position of the text you want to change the color of, to replace 15 and use the position of the last character from the String instead of 30. – Mike Oct 20 '15 at 21:24
  • Please check the edited answer. I wrapped up a generalized method for you to use... – Mike Oct 20 '15 at 21:33
0

you could define a String in your strings.xml file

 <string name="test2">&lt;font color=\'#FFFFFF\'>%1$s&lt;/font>  -- &lt;font color=\'#00FF00\'>%2$s&lt;/font></string>

and then programmatically

TextView tv = (TextView) findViewById(R.id.test);
 tv.setText(Html.fromHtml(getString(R.string.test2, String.valueOf(dateFormat.format(new Date(msg.getDate())), nameandemail)));
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • awesome that worked! thanks! do you also happen to have a clue about one of my other questions I am wondering about : http://stackoverflow.com/questions/33226491/how-do-i-display-the-calendar-date-on-the-top-of-chat-messages/33226608#33226608 –  Oct 21 '15 at 00:34