I have a TextView to be used as a bluetooth connection console. When I send a command, I want it to be written in a color (for example cyan), and the answers received in a different color (for example red).
Is it possible to do that, and if so, how?
I read it may be possible to do using HTML, but i'm not quite sure it is the best approach, or even how to do it.
Here's a little helper function based on C0deAttack's answer, that simplifies things
public static void appendColoredText(TextView tv, String text, int color) {
int start = tv.getText().length();
tv.append(text);
int end = tv.getText().length();
Spannable spannableText = (Spannable) tv.getText();
spannableText.setSpan(new ForegroundColorSpan(color), start, end, 0);
}
Just replace any calls to
textView.append("Text")
with
appendColoredText(textView, "Text", Color.RED);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With