Android adds padding across text
In my application, I take and parse the html content and display it using Spanned.
I use Spanned:
String html = "<p>Html Content</p>"
Spanned htmlSpan = Html.fromHtml(html, imageParser, null);
I’ve tried using the Apache Commons Library to set up padding but it didn’t work.
Is there any way to set up left and right padding?
Edit: Forgot to mention, I also have images in that html content. I tried adding padding to the TextView itself, but that way, all images also had fills.
Solution
In general, the easiest solution to
display HTML is to use WebView
. This is not the best solution, but it also works with CSS and JavaScript.
In TextView
, you can use <blockquote
to apply padding across text, > which Html.fromHtml()
will be converted to QuoteSpan
. You can replace it and add your own LeadingMarginSpan
implementation. It’s the same by adding a TagHandler
. Create and process your own tags on spannable strings.
You can search for and replace spans similar to the following
QuoteSpan[] spans = text.getSpans(0, text.length(), QuoteSpan.class);
text.setSpan(YOUR_SPAN, text.getSpanStart(spans[0]), text.getSpanEnd(spans[0]), 0);
There is little documentation and few tutorials using span, but you can check out this for further reading.