I am pretty new to Android. I am able to reproduce my issue with a very simple example.
I have the following in my strings.html
:
<string name="nice_html">
<![CDATA[
<div><p><strong><a href=\"https://www.reddit.com/r/nosleep/comments/3ijnt6/im_a_search_and_rescue_officer_for_the_us_forest/">Timeline</a> of my other stories separated by company.</strong></p><p><a href=\"https://redd.it/7jmvxo\">This</a> story about smashing modems from <a href=\"/u/devdevo1919\">u/devdevo1919</a> reminded of this little gem when I was a customer service representative at $SecurityCompany. Now before you say \u201cbut customer service isn\u2019t IT\u201d, let me explain the position. They <em>called</em> us CSRs, but what we really did was mostly technical support for the residential and small business security systems as well as some customer service functions. We had a separate team for the large businesses.</p></div>
]]>
</string>
My mainactivity.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pranapps.htmltest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/mylabel"
android:layout_marginTop="25dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
/>
</android.support.constraint.ConstraintLayout>
And the code for activity looks like:
TextView mylabel = (TextView) findViewById(R.id.mylabel);
String htmlstring = getString(R.string.nice_html);
Spannable spannable = (Spannable) Html.fromHtml(htmlstring);
System.out.println("htmlstring: "+htmlstring);
System.out.println("spannable: "+spannable);
mylabel.setText(spannable);
This is giving me the wrong output. Notice how the entire first line of the string is missing in the output and the entire string shows as bold.
The system.out
shows that the Spannable is the one causing the issue and it's trimming the entire first line for some reason. Why might it be doing that?
01-06 15:47:12.470 14719-14719/com.XXXXXX.htmltest I/System.out: htmlstring: <div><p><strong><a href="https://www.reddit.com/r/nosleep/comments/3ijnt6/im_a_search_and_rescue_officer_for_the_us_forest/>Timeline</a> of my other stories separated by company.</strong></p><p><a href="https://redd.it/7jmvxo">This</a> story about smashing modems from <a href="/u/devdevo1919">u/devdevo1919</a> reminded of this little gem when I was a customer service representative at $SecurityCompany. Now before you say “but customer service isn’t IT”, let me explain the position. They <em>called</em> us CSRs, but what we really did was mostly technical support for the residential and small business security systems as well as some customer service functions. We had a separate team for the large businesses.</p></div>
01-06 15:47:12.470 14719-14719/com.XXXXXX.htmltest I/System.out: spannable: This story about smashing modems from u/devdevo1919 reminded of this little gem when I was a customer service representative at $SecurityCompany. Now before you say “but customer service isn’t IT”, let me explain the position. They called us CSRs, but what we really did was mostly technical support for the residential and small business security systems as well as some customer service functions. We had a separate team for the large businesses.
I figured it out but I am curious whether this is a bug. If I remove just the very first \
after href=
tag, then it starts to work properly. Is this a bug?
Your HTML is in a CDATA block. AFAIK, you do not need to escape quotation marks there. But, if you do, you need to do so consistently, apparently.
Your second <a>
is:
<a href=\"https://redd.it/7jmvxo\">
Note that you have both quotation marks escaped with backslashes.
Your first <a>
is:
<a href=\"https://www.reddit.com/r/nosleep/comments/3ijnt6/im_a_search_and_rescue_officer_for_the_us_forest/">
Note that you have escaped the opening quotation mark, but not the closing quotation mark.
Your repaired <a>
, based on your comment, is:
<a href="https://www.reddit.com/r/nosleep/comments/3ijnt6/im_a_search_and_rescue_officer_for_the_us_forest/">
If that works, then you do not need to escape quotation marks here, as those are not escaped.
So, I would remove the escape backslashes, to simplify your life.