Search code examples
androidhtmlandroid-viewandroid-spannable

Spannable text that holding custom view


Is that possible to add a custom view into a spannable text?

In android.text.style package I can see many types of spannable objects, but I wonder if I can add a custom view.

Sort of spanable.setSpan(CustomView, .. , .. ,..)

Note that: CustomView maybe any sort of view i.e: WebView


Solution

  • So looks like it will be challenging to use Spannable way to handle such case.

    Therefore I created a new library to handle tags so I can use them wherever I want.

    And here is an example

    HtmlParser.Builder(StringSource(source)).setCallback(object : HtmlParser.ParserCallbacks {
    override fun onParseFinished(list: List<Element>) {
    list.forEach {
            Log.d(TAG, "onParseFinished: ${ElementType.values()[it.type]} ||| ${it.toString()}")
        if (it is ImageElement) {
            Log.d(TAG, "onImageFound: $it ||| ${it.ImageUrl}")
        } else if (it is IFrameElement) {
            Log.d(TAG, "onIFrameFound: $it ||| ${it.url}")
        } else if (it is BlockQuoteElement) {
            Log.d(TAG, "onBlockQuoteFound: ${it.data} ${it.text}")
        } else if (it is FigureElement) {
            Log.d(TAG, "onFigure: ${it.caption}  ${it.url}")
        }
     } 
     } override fun onParseError(exception: Exception) {}})
       .build()
    

    Now I can handle elements in whatever I want i.e: RecycleView