1

As the title states I am looking for a faster alternative than this:

textviewXy.setText(Html.fromHtml(random_html_styled_string));

this approach is really slow if you need to apply it to several textviews and i am looking for another method(maybe a support library), other than spannables (i tried them, but they wont work as the dont allow line breaks unless you use a spannableStringBuilder which isnt possible at the moment with my apps architecture...

i found a "similar" question here but i dont want to decode the html out of the string, i just want a faster API to apply it to my textview..

Thanks in advance

Community
  • 1
  • 1
1sm3t
  • 25
  • 7

1 Answers1

-2

i am looking for another method(maybe a support library)

Asking for off-site resource recommendations, like a library, is considered off-topic for Stack Overflow.

other than spannables

You have no choice but to use character spans for formatting in a TextView.

i tried them, but they wont work

Considering that Html.fromHtml() uses spans, clearly they work.

i just want a faster API to apply it to my textview

You are welcome to use Traceview to determine where your problem lies.

You are also welcome to create your own HTML-to-Spannable converter that does what Html.fromHtml() does. Perhaps you can find a way to do it faster than the stock implementation does. You could also search around to see if somebody else has created one of these — I am not aware of anything that is available.

Depending upon how you are using this stuff, you could pre-convert the HTML to Spannable objects using Html.fromHtml(). If you have a bunch of these, you could even consider doing that work in a background thread, so as not to tie up the main application thread.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Pre converting the spannables ist already what i am doing in a background thread, but it doesn't help at all. The Problem with implementing spannables immediately is that there is no way to insert a new line/break like with html tags. There is also no need to create a TraceView, because I know (through breakponts) that my UI hangs in the exact moment that the spannables are being applied to my textview (not like in the example above. I am thus only applying the preapplied spannable, thus no html.fromhtml there..)... – 1sm3t Jan 22 '15 at 14:06
  • @1sm3t: "The Problem with implementing spannables immediately is that there is no way to insert a new line/break like with html tags" -- again, `Html.fromHtml()` uses spans. "my UI hangs in the exact moment that the spannables are being applied to my textview" -- then there is nothing you can do, short of writing your own `TextView` replacement from scratch. – CommonsWare Jan 22 '15 at 14:23
  • Is fromhtml really unable to to convert around 200 tags in less than 5 seconds..? Maybe there is something else I am doing wrong.. will look into it later today.. – 1sm3t Jan 22 '15 at 14:32
  • @1sm3t: "Is fromhtml really unable to to convert around 200 tags in less than 5 seconds..?" -- according to you, that is not the problem. You said "my UI hangs in the exact moment that the spannables are being applied to my textview" and "I am thus only applying the preapplied spannable". In that case, `fromHtml()` already happened, as you already have the `Spannable`, and therefore it is not contributing to your problem. – CommonsWare Jan 22 '15 at 14:33
  • It hangs when I call... TextviewXY.setText(PreappliedSpannable); ..... I am really confused.. – 1sm3t Jan 22 '15 at 14:46
  • @1sm3t: And to be honest, I find that a bit surprising, as I would not expect `TextView` to do too much work right there. That's why I am suggesting the use of Traceview, as it may give you some more clues. – CommonsWare Jan 22 '15 at 14:49
  • So basically i did a TraceView and one can see that "something" took really long, but i am not able to decipher what really caused it... – 1sm3t Jan 22 '15 at 18:11
  • this is the first time i am watching -1 on @CommonsWare answer – Shubham AgaRwal Jun 24 '16 at 10:39
  • @CommonsWare can you please help me with https://stackoverflow.com/questions/55801039/fragment-savedinstancestate-is-always-null-when-using-navigation-component –  Apr 22 '19 at 21:27