Can I change the color of half of Android textview?

Asked 1 years ago, Updated 1 years ago, 86 views

If it's 10:15 as implemented in Everytime, I'd like to change it to only 1/4 color, which is 15 minutes, so how can I implement it? It is being implemented through textview.

android textview color table

2022-09-22 18:32

1 Answers

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.


2022-09-22 18:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.