1

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

Manikandan
  • 844
  • 15
  • 31

1 Answers1

0

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

Markus Dietrich
  • 618
  • 6
  • 18
  • yep. as mentioned, if you want to stick with your ReplacementSpan just factor in the y parameter of the Draw method in your canvas.DrawText. In your snippet you are ignoring it completely. This results in the text being drawn at the same y-offset, regardless of its line number. – Markus Dietrich Jun 11 '15 at 09:39
  • please check my updated question when i use y value are background span text look like this even i set extra line space – Manikandan Jun 11 '15 at 09:54
  • so the overlapping is gone - i assume you want the lines to have a small linepitch? – Markus Dietrich Jun 11 '15 at 10:07
  • How can i do sir, can you please help me. I spend more than 2 days for this – Manikandan Jun 11 '15 at 10:08
  • Substract an offset when drawing your background. Example: var rect = new RectF(x , top, x + paint.MeasureText(text, start, end)+8, bottom - 2); results in a 2px gap at the bottom – Markus Dietrich Jun 11 '15 at 10:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80269/discussion-between-manikandan-and-mdi1984). – Manikandan Jun 11 '15 at 10:20
  • its worked fine but if i need extend space between line what shall i do, because when i do bottom-5 or more bottom text is not covered with background – Manikandan Jun 11 '15 at 10:29
  • you have to increase the android:lineSpacingExtra="..px" in your TextView to compensate this – Markus Dietrich Jun 11 '15 at 11:21
  • there is no luck, i m going to move image span but its reduce the text size and not readable – Manikandan Jun 11 '15 at 12:23