2

I know I can use:

textview.setText(Html.fromHtml("html stuff"))

but that doesn't know how to handle lists and a lot of other elements

It also adds a lot of padding for some reason and overall seems like not a great solution.

I tried using WebViews in place of TextViews. However, that's extremely expensive, so it's not an option.

This seems like such a standard thing, there has to be a way to do this nicely or a library for it, or is there?

Community
  • 1
  • 1
SpaceMonkey
  • 4,143
  • 5
  • 38
  • 60
  • using a WebView is probably the easiest solution.. – Frame91 Jun 18 '15 at 00:06
  • @Frame91 That's in no way a solution, a WebView is not a replacement for a TextView, it slows down the app a lot when I have 5 WebViews in one page rendered at once – SpaceMonkey Jun 18 '15 at 00:07
  • http://stackoverflow.com/a/3150456/1484047 <-- see this link. Afaik ul, li are not supported by TextView. Using a WebView or parsing your text and insertig them in a listview would be your only solutions – Frame91 Jun 18 '15 at 00:09
  • This is definitely a gap in Android. I don't know of anything offhand. You can cast around on GitHub for something. I had a bunch of bullet list items defined in strings.xml and I got so tired of the clunky layout workarounds that I wrote my own TextView subclass just to handle the proper indenting for bullets. – kris larson Jun 18 '15 at 03:23
  • @kris I honestly don't know why android development is so terrible – SpaceMonkey Jun 18 '15 at 08:16
  • I posted some code in an answer, maybe that will help – kris larson Jun 18 '15 at 15:57

2 Answers2

1

Here's some code from my TextView subclass for handling bullets, I left out the easy part.

@Override
public void setText(CharSequence text, BufferType type) {

    StringBuilder sb = new StringBuilder();
    List<Integer> markers = new ArrayList<Integer>();

    /*
     *  Code to parse through text, remove tags, build up modified text in 
     *  StringBuilder and mark the bullet paragraph boundaries goes here.
     *  Left as an exercise for the reader.
     */

    SpannableString spannableString = new SpannableString(sb.toString());
    for (int i = 0; i < markers.size(); i += 2) {
        int start = markers.get(i);
        int end = markers.get(i+1);
        spannableString.setSpan(new BulletSpan(bulletGap), start, end, Spannable.SPAN_PARAGRAPH);
    }

    super.setText(spannableString, BufferType.SPANNABLE);
}

Note: If I remember correctly, Android really really wants that Paragraph Span to end with a newline character.

Also I tried but couldn't figure out how to use LineHeightSpan to make the first and last lines of the bullet span paragraph a little higher.

If you want a number (ordered) list, you're on your own.

kris larson
  • 30,387
  • 5
  • 62
  • 74
0

The solution is to implement a TagHandler and pass it to fromHtml. There is already a nice one here:

https://github.com/sufficientlysecure/html-textview

The only problem is that there will be some extra space at the end for w/e reason. You can write a simple function to trim those extra newlines.

SpaceMonkey
  • 4,143
  • 5
  • 38
  • 60