Android Set Image in TextView Through Spanned String

July 22, 2018

The example below replace * is string with an drawable image.

val googleText = SpannableStringBuilder("* Hello").apply {
    setSpan(ImageSpan(context, R.drawable.powered_by_google_dark), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

textView.text = googleText
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.