If you want to color it by text unit, there are two ways.
(The part marked [ ] must be changed when writing the code)
- Use Html
// kotlin code
val htmlText = Html.fromText("<font color='#[hex color1]'></font><font color='#[hex color2]'>">"
[Text View].text = if(Build.VERSION.SDK_INT < 24) @Suppress("DEPRECATION") Html.fromHtml(htmlText) else Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT)
// Java code
String htmlText = Html.from Html ("<font color='#[hex color1]'></font><font color='#[hex color2]'>"
f(Build.VERSION.SDK_INT < 24) @SuppressWarnings ("deprecation") [Text View].setText(Html.fromHtml(htmlText));
else [text view].setText(Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT));
It's very basic HTML tags (b, br, p, em, etc.) that are taken and converted into something similar to the SpanableString that I'm going to introduce below. (Is it Spanned?) " " You can put 'very basic HTML tags' inside.
- Use SpanableString
// kotlin code
SpanableString text = SpanableString ([Text])
text.setSpan (ForegroundColorSpan), [Start Position], [End Position], Spanned.SPAN_INTERMEDIATE)
text.setSpan (ForegroundColorSpan), [Start Position], [End Position], Spanned.SPAN_INTERMEDIATE)
[Text View].text = text
// ...
If you use ktx,
text.setSpan (ForegroundColorSpan ([color code]), [Start position], [End position], Spanned.SPAN_INTERMEDIATE)
-> text[[Start Location]..[End Location] = ForegroundColorSpan ([Color Code])
(for example, text[0..4] = ForegroundColorSpan (Color.rgb (12,34,56))
).
// java code
SpanableString text = new SpanableString ([text]);
text.setSpan(new ForegroundColorSpan(), [Start Position], [End Position], Spanned.SPAN_INTERMEDIATE);
text.setSpan(new ForegroundColorSpan(), [Start Position], [End Position], Spanned.SPAN_INTERMEDIATE);
// ...
[Text View].setText(text);
There are many other types besides Foreground ColorSpan, so you can use them.
© 2024 OneMinuteCode. All rights reserved.