Hide string content in a span

Viewed 147

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
        }
    }
1 Answers

Not sure if this will give you exactly what you want, but generally displaying markup language in a TextView or EditText(?) can be done in this way:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    textView.setText(Html.fromHtml("@<m id="36c03920-f411-4919-a175-a5b1eb616592">Full name</m>, how are you doing?", Html.FROM_HTML_MODE_COMPACT));
} else { 
    textView.setText(Html.fromHtml("@<m id="36c03920-f411-4919-a175-a5b1eb616592">Full name</m>, how are you doing?")); 
Related