Search code examples
androidtextviewspannablestringspannable

Android SpannableString set background behind part of text


I would like to create something similar as seen on this image: enter image description here

I managed to create evertyhing with SpannableStringBuilder, except the orange rounded rectangle. I can set the background to that color with BackgroundColorSpan, but I can't find a way to make it rounded. Any ideas how can I achieve this?

Thanks in advance!

EDIT: I'm using Xamarin.Android, but here is my code:

stringBuilder.SetSpan(new BackgroundColorSpan(Application.Context.Resources.GetColor(Resource.Color.orangeColor)), stringBuilder.Length() - length, stringBuilder.Length(), SpanTypes.ExclusiveExclusive);

Solution

  • I managed to solve my problem, based on pskink's suggestion. Here is my class:

    public class RoundedBackgroundSpan : ReplacementSpan
    {
        public override void Draw(Canvas canvas, ICharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
        {
            var rect = new RectF(x, top, x + MeasureText(paint, text, start, end), bottom);
            paint.Color = Application.Context.Resources.GetColor(Resource.Color.nextTimeBackgroundColor);
            canvas.DrawRoundRect(rect, Application.Context.Resources.GetDimensionPixelSize(Resource.Dimension.localRouteDetailsRoundRectValue), Application.Context.Resources.GetDimensionPixelSize(Resource.Dimension.localRouteDetailsRoundRectValue), paint);
            paint.Color = Application.Context.Resources.GetColor(Resource.Color.nextTimeTextColor);
            canvas.DrawText(text, start, end, x, y, paint);
        }
    
        public override int GetSize(Paint paint, ICharSequence text, int start, int end, Paint.FontMetricsInt fm)
        {
            return Math.Round(MeasureText(paint, text, start, end));
        }
    
        private float MeasureText(Paint paint, ICharSequence text, int start, int end)
        {
            return paint.MeasureText(text, start, end);
        }
    }
    

    Example usage:

    var stringBuilder = new SpannableStringBuilder();
    var stringToAppend = "hello world";
    stringBuilder.Append(stringToAppend);
    stringBuilder.SetSpan(new RoundedBackgroundSpan(), stringBuilder.Length() - stringToAppend.Length, stringBuilder.Length(), SpanTypes.ExclusiveExclusive);