Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I took janoliver's answer and came up with my version that attempts to support more options</p> <pre><code> String text = ""; // HTML text to convert // Preprocessing phase to set up for HTML.fromHtml(...) text = text.replaceAll("&lt;span style=\"(?:color: (#[a-fA-F\\d]{6})?; )?(?:font-family: (.*?); )?(?:font-size: (.*?);)? ?\"&gt;(.*?)&lt;/span&gt;", "&lt;font color=\"$1\" face=\"$2\" size=\"$3\"&gt;$4&lt;/font&gt;"); text = text.replaceAll("(?&lt;=&lt;font color=\"#[a-fA-F0-9]{6}\" )face=\"'(.*?)', .*?\"", "face=\"$1\""); text = text.replaceAll("(?&lt;=&lt;font color=\"#[a-fA-F0-9]{6}\" )(face=\".*?\" )size=\"xx-small\"", "$1size=\"1\""); text = text.replaceAll("(?&lt;=&lt;font color=\"#[a-fA-F0-9]{6}\" )(face=\".*?\" )size=\"x-small\"", "$1size=\"2\""); text = text.replaceAll("(?&lt;=&lt;font color=\"#[a-fA-F0-9]{6}\" )(face=\".*?\" )size=\"small\"", "$1size=\"3\""); text = text.replaceAll("(?&lt;=&lt;font color=\"#[a-fA-F0-9]{6}\" )(face=\".*?\" )size=\"medium\"", "$1size=\"4\""); text = text.replaceAll("(?&lt;=&lt;font color=\"#[a-fA-F0-9]{6}\" )(face=\".*?\" )size=\"large\"", "$1size=\"5\""); text = text.replaceAll("(?&lt;=&lt;font color=\"#[a-fA-F0-9]{6}\" )(face=\".*?\" )size=\"x-large\"", "$1size=\"6\""); text = text.replaceAll("(?&lt;=&lt;font color=\"#[a-fA-F0-9]{6}\" )(face=\".*?\" )size=\"xx-large\"", "$1size=\"7\""); text = text.replaceAll("&lt;strong&gt;(.*?)&lt;/strong&gt;", "&lt;_em&gt;$1&lt;/_em&gt;"); // we use strong for bold-face text = text.replaceAll("&lt;em&gt;(.*?)&lt;/em&gt;", "&lt;strong&gt;$1&lt;/strong&gt;"); // and em for italics text = text.replaceAll("&lt;_em&gt;(.*?)&lt;/_em&gt;", "&lt;em&gt;$1&lt;/em&gt;"); // but Android uses em for bold-face text = text.replaceAll("&lt;span style=\"background-color: #([a-fA-F0-9]{6}).*?&gt;(.*?)&lt;/span&gt;", "&lt;_$1&gt;$2&lt;/_$1&gt;"); text_view.setText(Html.fromHtml(text, null, new Html.TagHandler() { private List&lt;Object&gt; _format_stack = new LinkedList&lt;Object&gt;(); @Override public void handleTag(boolean open_tag, String tag, Editable output, XMLReader _) { if (tag.startsWith("ul")) processBullet(open_tag, output); else if (tag.matches(".[a-fA-F0-9]{6}")) processBackgroundColor(open_tag, output, tag.substring(1)); } private void processBullet(boolean open_tag, Editable output) { final int length = output.length(); if (open_tag) { final Object format = new BulletSpan(BulletSpan.STANDARD_GAP_WIDTH); _format_stack.add(format); output.setSpan(format, length, length, Spanned.SPAN_MARK_MARK); } else { applySpan(output, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } private void processBackgroundColor(boolean open_tag, Editable output, String color) { final int length = output.length(); if (open_tag) { final Object format = new BackgroundColorSpan(Color.parseColor('#' + color)); _format_stack.add(format); output.setSpan(format, length, length, Spanned.SPAN_MARK_MARK); } else { applySpan(output, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } private Object getLast(Editable text, Class kind) { @SuppressWarnings("unchecked") final Object[] spans = text.getSpans(0, text.length(), kind); if (spans.length != 0) for (int i = spans.length; i &gt; 0; i--) if (text.getSpanFlags(spans[i-1]) == Spannable.SPAN_MARK_MARK) return spans[i-1]; return null; } private void applySpan(Editable output, int length, int flags) { if (_format_stack.isEmpty()) return; final Object format = _format_stack.remove(0); final Object span = getLast(output, format.getClass()); final int where = output.getSpanStart(span); output.removeSpan(span); if (where != length) output.setSpan(format, where, length, flags); } })); </code></pre> <p>This does seem to get the bullets, foreground color, and background color. It might work for the font-face but you might need to supply the fonts as it doesn't seem that Android supports fonts other than Droid/Roboto. </p> <p>This is more of a proof-of-concept, you might probably want to convert the regex into <code>String</code> processing, since regex doesn't support combining the preprocessing in any way, meaning this takes a lot of passes over the <code>String</code>. This also doesn't seem to get the font size to change, I've tried defining it like "16sp", "medium", or "4" without seeing changes. If anyone has gotten sizes to work, mind sharing?</p> <p>I currently would like to be able to add numbered/ordered list support to this, i.e.</p> <ol> <li>item</li> <li>item</li> <li>item</li> </ol> <p><strong>NOTE:</strong> To people starting with any of this, it seems that the "tag" that is given to <code>handleTag(...)</code> is just the name of the tag (like "span"), and doesn't contain any of the attributes assigned in the tag (like if you have "), you can see my loophole around this for the background color.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload