0

I need to create a custom Span that renders its content like source code. Here's an example of what I am trying to achieve. Let's say we have the following text, where I have annotated the start and end of the custom span using square brackets:

Some text [some\nmulti line\ntext] some more text

The prefix ("Some text") and suffix ("some more text") should be rendered normally, but the custom span should be rendered like a code block:

some
multi line
text

I have been looking at using ReplacementSpan for this, since i Have previously used that to create various interesting custom spans, but for this case I just can't get it to do what I want.

I somehow need to get my span to render a full block of graphics, occupying the entire width of the container. Is it even possible to do this?

Elias Mårtenson
  • 3,820
  • 23
  • 32
  • use `android.text.style.LineBackgroundSpan` – pskink Mar 30 '16 at 05:38
  • @pskink I have tried that, but while that will allow me to draw the background, it will break up my span into three separate draw calls (I presume the newlines causes the source text to be split into separate strings even before `LineBackgroundSPan` gets a chance to look at the text)? – Elias Mårtenson Mar 30 '16 at 05:52
  • yes it "breaks up your span into three separate draw calls", what is the problem with that? what do you want to achieve in "draw" method? – pskink Mar 30 '16 at 06:04
  • textview.setSingleLine(false); textview.setText("some\n"+"multi line\n"+"text"); – Chaudhary Amar Mar 30 '16 at 06:37
  • @pskink it's a problem because I want to draw a single box around the entire block. – Elias Mårtenson Mar 30 '16 at 07:52
  • so do that: what is the problem? – pskink Mar 30 '16 at 07:53
  • @pskink that's my problem. The draw method is called three times with different parts of the string. At least with `LineBackgroundSpan` there is no way to treat all three lines of text as a single unit. – Elias Mårtenson Mar 30 '16 at 10:32
  • try this: http://pastebin.com/SxaqL4jh – pskink Mar 30 '16 at 12:32
  • Interesting idea. I'll play around with it a but. Thank you for the idea. – Elias Mårtenson Mar 30 '16 at 16:33
  • @pskink, the problem is all lines backgrounds are dwawn separately, each with it's own width. But I need do draw a rectangle that contains all three lines, with a with of a widest line (or view's width). – babay Aug 26 '17 at 02:18
  • @babay then use `android.text.Layout` api – pskink Aug 26 '17 at 04:48

1 Answers1

0

Android processes multiline spans line-by-line. The only way I've found is to override TextView.onDraw, compose blocks for each block-span and draw them before (of after, if you want) TextView.onDraw.

Here is an example activity https://github.com/Babay88/AndroidCodeSamplesB/blob/master/blockquotespanexample/src/main/java/ru/babay/blockquotespanexample/MainActivity.java

It uses customized TextView https://github.com/Babay88/AndroidCodeSamplesB/blob/master/blockquotespan/src/main/java/ru/babay/blockquotespan/TextViewWithBlockBackgrounds.java

And special BlockQuoteSpan.

You should ensure that each block span starts and ends with a \n.

babay
  • 4,689
  • 1
  • 26
  • 40