How to set a Custom View (not an image) at the end of a multiline TextView in Android?

92 Views Asked by At

I am trying to add a custom view (the portion with red boundary will be replaced with my custom view having a 3 dots loader animation) at the end of a multi-line TextView in my Android app.

enter image description here

I know how to specifically add an ImageView using a SpannableString like this:-

fun addImageToEndOfTheString(text: String, drawableResourceId : Int ,context: Context) : SpannableStringBuilder {
        val drawable = ContextCompat.getDrawable(context, drawableResourceId)!!
        drawable.setBounds(0, 0, 98, 50)
        val rocketImageSpan = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)

        val ssBuilder = SpannableStringBuilder(text)

        ssBuilder.setSpan(
            rocketImageSpan,
            text.length-1,
            text.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        return ssBuilder
    }

But the problem is I have a custom view which I want to place at the end of my TextView. How can I achieve that?

Note : My custom view is a subclass of View.

1

There are 1 best solutions below

2
snachmsm On

simply: you can't.

in posted example you aren't adding ImageView, you are adding ImageSpan with just drawable, thats very not the same...

Views can't hold/host other Views, you have to use ViewGroup or some subclass

easiest way would be to make some LinearLayout with vertical orientation and two Views: TextView and your custom View. second one may have android:visibility="gone" and you can show it only when needed