1

I have created a custom EditText class which draws line numbers next on the left of every line. This works fine however I also want to set background of the line numbers to grey and achieve something like this:

what i want it to look like

In onDraw method where I draw the numbers I tried to draw really thick line but it always gets drawn over the line numbers no matter where I put it, before the call the draws the line numbers or after. Here is my code

protected void onDraw(Canvas canvas) {
    int baseline = getBaseline();

    for (int i = 0; i < getLineCount(); i++) {
        //line still gets drawn over the text
        canvas.drawText("" + (i + 1), rect.left, baseline, paint);
        canvas.drawLine(rect.left,baseline,rect.right,rect.top,fill);
        canvas.drawText("" + (i + 1), rect.left, baseline, paint);

        baseline += getLineHeight();

    }

    super.onDraw(canvas);
}

Any suggestions about how to make the line appear in the background?

Thanks

John Smith
  • 844
  • 8
  • 26
  • 1
    I don't see where you're ever updating `rect`, so I would guess that it's a new line's `drawLine()` drawing over a previous line's number. Why don't you just use `drawRect()` for the background, and update `rect` for each line? – Mike M. Jun 01 '16 at 20:09
  • Thanks looking a bit deeper that is exactly what I did. – John Smith Jun 06 '16 at 13:10

1 Answers1

0

Try to draw a line at which the width of the line number. before the loop:

Paint linePaint = new Paint(getPaint()); // copy original Paint
linePaint.setARGB(255, 200, 200, 200); // some grey color
String strCount = String.valueOf(getLineCount());

float[] symbolWidths = new float[strCount.length()];
linePaint.getTextWidths(strCount, symbolWidths);

float strokeWidth = 0;
for (float width : symbolWidths)
    strokeWidth += width;
strokeWidth = strokeWidth *2/*I dnt knw y*/ + strokeWidth;
linePaint.setStrokeWidth(strokeWidth);
setPadding((int)strokeWidth / 2, 0, 0, 0); // text padding

canvas.drawLine(rect.left, getLineHeight() * getLineCount(), rect.right, rect.top, linePaint);