Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place a view at the end of multiline TextView

I want to add a custom view (a view with both text and icon) at the end of dynamic TextView like in this picture.

Thanks in advance.

enter image description here

like image 255
Hai Hack Avatar asked Oct 12 '25 08:10

Hai Hack


1 Answers

It is possible programmatically:

fun TextView.setIconifiedText(text: String, @DrawableRes iconResId: Int) {
    SpannableStringBuilder("$text#").apply {
        setSpan(
            ImageSpan(context, iconResId, DynamicDrawableSpan.ALIGN_BOTTOM),
            text.length,
            text.length + 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
    }.let {
        setText(it)
    }
}
textView.setIconifiedText(
    "First line text that is longer.\nSecond line text.",
    R.drawable.ic_android_black_24dp
)

Result:

like image 68
aminography Avatar answered Oct 14 '25 00:10

aminography



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!