android – Building a custom span that occupies the full width of the container-ThrowExceptions

Exception or error:

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?

How to solve:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *