$someString " /> $someString " /> $someString "/>

Spanned CSS style/color

610 views Asked by At

Is there any way to use spanned colors directly from a string without using any Java/Kotlin code?
For example to bold text I can use "<b> $someString </b>"
But what is the way to change color? I searched it in Android Documentation and I found this.

CSS style: <span style=”color|background_color|text-decoration”>

But I don't get it how to set it, I tried to do it in many ways e.g.
"<span style=#999999|#999999|#999999> $someString </span>"
But it didn't work.

2

There are 2 answers

3
M D P On BEST ANSWER

The character | means or.

In the documentation you see <span style=”color|background_color|text-decoration”>

Which means it can be any of <span style="color:#999999"> $someString </span>

Or <span style="background-color:#999999"> $someString </span>

Or <span style="text-decoration:underline"> $someString </span>

0
Scott Johnson On

Luckily i just had to do this for work! What i did was use

Html.fromHtml(""<b> $someString </b>"", Html.FROM_HTML_MODE_LEGACY);

The above is API v24 and above, to do this on older android Devices you would need to use

SpannableString spannableString = new SpannableString("Some Text");

To change the appearance You can use

spannableString.setSpan(new RelativeSizeSpan(2f), 0, 2, SpannableString.SPAN_EXCLUSIVE_INCLUSIVE); That will increase the size of characters 0-2 in the string you provided, by 2 fold.

Which for me resulted in: Example SpannableString