ReplacementSpan in multiautocomplete textview overlap

992 views Asked by At

Hi i have multiple autocomplete text view with space tokenizer and use ReplacementSpan for background color change in each contacts

my custom replacement code is

public class MyForgroudSpan : 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 + paint.MeasureText(text, start, end)+8, bottom);
        paint.Color = Android.Graphics.Color.ParseColor("#E5E5E6");
        canvas.DrawRect(rect, paint);
        paint.Color = Android.Graphics.Color.Black;
        int xPos = Java.Lang.Math.Round(x + (8 / 2));
        int yPos = (int)((canvas.Height / 2) - ((paint.Descent() + paint.Ascent()) / 2));

        canvas.DrawText(text, start, end, xPos, yPos, paint);

    }
    public override int GetSize(Paint paint, ICharSequence text, int start, int end, Paint.FontMetricsInt fm)
    {
        return Java.Lang.Math.Round(paint.MeasureText(text, start, end))+8;
    }
}

i set spannable string here

SpannableStringBuilder ssb = new SpannableStringBuilder(Text.trim());
ssb.SetSpan(new MyForgroudSpan(), x, x + c.Length, SpanTypes.ExclusiveExclusive);

its ok when multiple auto complete textview have single line but it come to multiple line means it overlap each text

please see the screens

1.single line

single line image

2.multiline image

Multiline image

When i use y value like this

canvas.DrawText(text, start, end, xPos, y, paint);

lyout like this no vertical space between line

1

There are 1 answers

9
Markus Dietrich On

If you are using your ReplacementSpan only to change the background color you could as well simply use a BackgroundColorSpan:

ssb.SetSpan(
  new BackgroundColorSpan(
    Android.Graphics.Color.ParseColor("#E5E5E6"), 
    x,
    x + c.Length, 
    SpanTypes.ExclusiveExclusive));

The overlap problem occurs because you are not using the y value in your DrawText Method. If you replace it with

canvas.DrawText(text, start, end, xPos, y, paint);

it should look proper