0

I saw this topic that tells me how to display HTML text in a TextView in Java. For example, if my TextView has "<small>Hello</small>" as its text and I used the code in that topic, I would see a small "Hello" in my TextView.

Now I want to reverse this. I want to get the HTML code of the TextView that I have enabled HTML format, i.e., I want to get "<small>Hello!</small>". Is there a way to do this?

I have tried tv.getText() (tv is the name of the TextView), but it instead returned "Hello", not as I had expected.

  • _"I have tried tv.getText() (tv is the name of the TextView), but it instead returned "Hello", not as I had expected"_. You haven't shown us the actual code, but if you assigned the return value to a `String` then that result doesn't seem surprising. Have you tried casting the value to `Spannable` and then passing it to `HtmlCompat.toHtml`? – Michael Mar 28 '22 at 06:19
  • Hi Michael, thanks for the reply. I tried tv.getText().toString(), but it did not work. Can I try to cast the value to SpannableStringBuilder, then pass it to HtmlCompat.toHtml? –  Mar 28 '22 at 06:27
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 28 '22 at 06:54
  • Html.toHtml(tv.getText()) will be the closest you can get. Please note that it may not be exactly the same as the original input- you're regenerating new HTML, because the original HTML was lost after you turned it into a Spannable. It isn't saved because Android's UI doesn't work by displaying HTML. – Gabe Sechan Mar 28 '22 at 07:02

0 Answers0