I have EditText with content: @<m id="36c03920-f411-4919-a175-a5b1eb616592">Full name</m>, how are you doing?. I would like to hide tag with id from the user and keep only @Full name, how are you doing? when displaying text. Still getText should return full content.
I found ReplacementSpan useful to this approach. At first step, tried replacing only </m> but text is drawn twice in two lines. First line starts with @ and second one starts with <. Also cannot insert new text. getSize is called multiple times and draw is called twice.
Is my approach correct? Should I try to found different solution and store ids in separate collection and do post processing on getText()?
Code:
inner class RemoveTagSpan : ReplacementSpan() {
private val regexEndTag = Regex("</m>")
override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
text?.replace(regexEndTag, "")?.let { canvas.drawText(it, start, it.length - 1, x, y.toFloat(), paint) }
}
override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: FontMetricsInt?): Int {
return text?.replace(regexEndTag, "")?.let { paint.measureText(it).roundToInt() } ?: 0
}
}