Hai Hack
Hai Hack

Reputation: 1018

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

Upvotes: 4

Views: 1447

Answers (3)

Khaled Alramam
Khaled Alramam

Reputation: 47

Place both of them in relative layout and add this

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

to the custom view

Upvotes: -3

aminography
aminography

Reputation: 22832

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:

Upvotes: 8

Egor Prokofyev
Egor Prokofyev

Reputation: 65

You need to use android:drawableEnd

Upvotes: -2

Related Questions