HTML with sub and sub-sub bullets not showing properly - android

Consider this HTML:
<ul>
<li>Some bullet point text.</li>
<li>Another bullet point:</li>
<ul>
<li>A Sub Bullet point of the above.</li>
<ul>
<li>A Sub sub bullet point.</li>
</ul>
<li>The second sub bullet point.</li>
</ul>
</ul>
I'm loading this into a TextView in MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val someText = findViewById<TextView>(R.id.someTextView)
val url = "<ul>\n" +
" <li>Some bullet point text.</li>\n" +
" <li>Another bullet point:</li>\n" +
" <ul>\n" +
" <li>A Sub Bullet point of the above.</li>\n" +
" <ul>\n" +
" <li>A Sub sub bullet point.</li>\n" +
" </ul>\n" +
" <li>The second sub bullet point.</li>\n" +
" </ul>\n" +
"</ul>\n"
someText.text = Html.fromHtml(url, Html.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE)
}
Nothing special with the activity_main.xml:
<TextView
android:id="#+id/someTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Expected (Sub, Sub-sub bullet points):
Actual (no sub and sub-sub bullets AND indenting is wrong):
I've tried this library but it has the same problems.
Another solution would be to convert the tags to "\u25CF" and sub-sub bullets to "\u25CB" (or BulletSpan) and use JSoup for the ordering. But this feels like a lot of work for a standard HTML text.
I've tried MyTagHandler but the same problem.
Any ideas?
Thanks for the help guys!

ul and li tags are not supported by Html.fromHtml() method. There is an open issue in Google Issue Tracker about these missing tag supports and about the supported tags not listen on Html.fromHtml() method's documentation.
But, I think BulletSpan might be helpful.

Related

How to differentiate two HTML elements with the same class?

I would like to know if there is any method in Jsoup that differentiates multiple elements with the same class. For clarification, consider the following HTML fragment, I need to retrieve the class name "description", but I need to differentiate one information from the other.
<Div class = "related-box gray-text no-margin">
<H3 class = "epsilon"> Awards </ h3>
<P class = "description">
<Strong> Sena - 6 </ strong> <br>
There was no
</ P>
<P class = "description">
<Strong> Quina - 5 </ strong> <br>
124 winning bets, R $ 43,174.39
</ P>
<P class = "description">
<Strong> Quadra - 4 </ strong> <br>
8817 winning bets,
</ P>
Thank You!
Assuming you want to grab the second <p>tag in the posted div, you have multiple options. The following examples use the nth-of-type CSS selector, the get function for jsoup Elements (inherited from ArrayList) and iterating through the elements, if you need more detailed comparison of the elements contents. There are a lot more options and as mentioned in my comment above, using tools like the chrome developer tools, you can select elements and get a matching selector, which might be a good starting point for generalization.
Example Code
String source ="<div class='related-box gray-text no-margin'>"+
"<h3 class='epsilon'>Awards</h3>"
+ "<p class='description'><strong>Sena - 6</strong><br>There was no</p>"
+ "<p class='description'><strong>Quina - 5</strong><br>124 winning bets, R $ 43,174.39</p>"
+ "<p class='description'><strong>Quadra - 4</strong><br>8817 winning bets,</p></div>";
Document doc = Jsoup.parse(source, "UTF-8");
// nth-of-type(n) CSS selector
Element quina = doc.select(".description:nth-of-type(2)").first();
System.out.println(quina.text());
// Elements.get(n) jsoup method
quina = doc.select(".description").get(1);
System.out.println(quina.text());
// iterate over Elements
Elements descriptions = doc.select(".description");
for (Element element : descriptions) {
if(element.text().contains("Quina")){
quina = element;
}
}
System.out.println(quina.text());
Output
Quina - 5 124 winning bets, R $ 43,174.39
Quina - 5 124 winning bets, R $ 43,174.39
Quina - 5 124 winning bets, R $ 43,174.39

SimpleXML Parser android getting HTML TAG

I have a TAG getinng the whole description is getting difficult in SimpleXML what property should i give to get whole description.
SimpleXML is parssing the p tag also .
<description>
<p>
Bar In Inconel</p> <ul> <li>600</li> <li>601</li> <li>617</li> <li>625</li> <li>718/718 NACE MRO</li> <li>725</li> <li>800</li> <li>825</li> <li>925</li> <li>K500</li> <li>MP35N</li> <li>X750</li> <li>C276</li></ul> <p>Aluminum Tubing, Aluminum Pipe, Aluminum Sheet, Aluminum Bar, Aluminum Plate In Aluminum</p> <ul> <li>2219</li> <li>2024</li> <li>2124</li> <li>3003</li> <li>5052</li> <li>6061</li> <li>7075</li> <li>7050</li></ul> <p>Stainless Steel In Tube, Pipe, Plate, Sheet &
</p>
</description>
When getting final response just enclose the CDATA tag and make the data=true for that element
str = str.replaceAll("<description>","<description><![CDATA[");
str = str.replace("</description>", "]]></description>");
:D :D
:D

Setting right-to-left direction for Hebrew in an android app

I write an application in Hebrew but there is a problem with the direction of the words:
This is the code:
textView2.setText("יצרן: " + company + "\n");
textView2.append("דגם: " + degem + "\n");
textView2.append("מנוע: " + manoa + "\n");
textView2.append("ק'מ/ליטר: "+ watt + "\n");
textView2.append("נסיעה: " + timeString + " " + timeMedida+"\n");
I got:
יצרן:ABARTH
instead of
ABARTH:יצרן
and
דגם:005
instead of
דגם:500
the xml:
<TextView
android:id="#+id/txt_1_delek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="50sp"
android:layout_marginRight="100sp"
android:layout_marginLeft="100sp"
android:text="myTextView"/>
In case someone stumbles upon this old question and is looking for an answer... Amir's link points toward the right direction.
What you need is to change the text's base direction (and not the alignment, so as explained in the link "gravity.RIGHT" wont help there). Inserting the "left mark" unicode character \u200F at the beginning of your string will do the trick.
BTW just adding it as a hardcoded character didn't work for me, apparently because of encoding issues. I prefered to avoid dealing with it in the code, so i simply extract the character as a string from strings.xml, let android do the conversion as it wishes, and append the string (in your case "יצרן: " + company + "\n") in realtime.

Add line break in Twitter (Android/Java)

I want to tweet some text to Twitter using line breaks.
I've tried \n or \r like in my code below, but Twitter is ignoring those.
tweet.setText(" Text1 " + "\n" + "Text2");
Thanks in advance!
try <br /> or [br /].
<br /> is the standard html tag, sometimes websites and forums use [br /].

Parsing issues while parsing xml response android

Xml response is like , but in one tag , the text is this:
<Description>
<center><strong><span>Warehouse / Building Maintenance</span></strong></center><br />
<br />
<strong><span>I</span></strong><span>mmediate openings available in the local Perris area for warehouse/building building maintenance positions. <br />
<br />
<strong>Job Description:</strong><br />
</span>
<ul>
<li><span>Associates will be responsible to define pieces of equipment that will paralyze operations if they fail, and plan whatever level of preventative maintenance necessary. <br />
</span></li>
<li><span&gt
......
similar text
......
</Description>
I can't able to parse it in proper way.
I tried using Jsoup.parse((nodeValue))
and Html.fromHtml(String) also URLEncoder.encode(String)
but its returning simple & symbol thats it.
How to parse this type of response?
A temporary solution could be applying replaceAll("<", "<").replaceAll(">", ">") if API methods aren't working, but arent you supposed to use Html.toHtml(string) when you have stuff encoded and want it to become real html ?
Actually its returning only & symbol , that's because the nodelist item returns only single value.
After using like this:
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("nodefstnm"+fstNm.getLength());
for(int j=0;j<fstNm.getLength();j++) {
String val = ((Node) fstNm.item(j)).getNodeValue();
nodeValue=nodeValue.concat(val);
}
and then parsing with Jsoup., returns what I want.

Categories

Resources