Skip to content

Instantly share code, notes, and snippets.

@SurajBahadur
Created February 3, 2021 11:43
Show Gist options
  • Save SurajBahadur/f73bfaa0330f7e132e778b394805ede1 to your computer and use it in GitHub Desktop.
Save SurajBahadur/f73bfaa0330f7e132e778b394805ede1 to your computer and use it in GitHub Desktop.
Nicest way to add rounded background span in TextView
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.text.style.ReplacementSpan
import kotlin.math.roundToInt
class RoundedBackgroundSpan(
private val textColor: Int,
private val backgroundColor: Int
) : ReplacementSpan() {
private val additionalPadding = 4.toPx().toFloat()
private val cornerRadius = 4.toPx().toFloat()
override fun draw(
canvas: Canvas,
text: CharSequence,
start: Int,
end: Int,
x: Float,
top: Int,
y: Int,
bottom: Int,
paint: Paint
) {
val newTop = y + paint.fontMetrics.ascent
val newBottom = y + paint.fontMetrics.descent
val rect = RectF(x, newTop, x + measureText(paint, text, start, end) + 2 * additionalPadding, newBottom)
paint.color = backgroundColor
canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint)
paint.color = textColor
canvas.drawText(text, start, end, x + additionalPadding, y.toFloat(), paint)
}
override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
return (paint.measureText(text, start, end) + 2 * additionalPadding).toInt()
}
private fun measureText(paint: Paint, text: CharSequence, start: Int, end: Int): Float {
return paint.measureText(text, start, end)
}
private fun Int.toPx(): Int {
val resources = Resources.getSystem()
val metrics = resources.displayMetrics
return (this * (metrics.densityDpi / 160.0f)).roundToInt()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment