I added an image in the middle of the TextView
using ImageSpan
But sometimes the text wraps the last word to the next line. And the last word is displayed separately from the icon, which should be with the text.
for example: here 6470.3 mi and location icon should be together
Does it possible to fix it?
My code:
val title = venue?.name + (if (!venue?.venue_name_indicator.isNullOrEmpty()) " - ${venue?.venue_name_indicator}" else "")
val span = SpannableStringBuilder(title)
if(venue?.distance != null && venue.distance.isNotEmpty()) {
val icon = ContextCompat.getDrawable(context, R.drawable.ic_location)
icon?.setBounds(0, 0, 32, 32)
icon?.setTint(ContextCompat.getColor(context, R.color.text_color_secondary))
span.append(" ")
.appendImage(icon!!)
.appendColor(context, context.getString(R.string.distance, venue.distance), R.color.text_color_secondary)
}
span.setSpan(StyleSpan(Typeface.BOLD), 0, title.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
venue_title?.text = span
method appendImage():
fun SpannableStringBuilder.appendImage(
image: Drawable
): SpannableStringBuilder {
val index = length
append("\\u00A0")
setSpan(ImageSpan(image), index, length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
return this
}
As you see im using non-breakable space \\u00A0
as a placeholder but that doesn't help