Place a view at the end of multiline TextView

Viewed 655

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

3 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:

You need to use android:drawableEnd

Place both of them in relative layout and add this

android:layout_toEndOf="<id of first view>"

to the custom view

Related