0

Going over this SO answer Android: Ruled/horizonal lines in Textview and some code from the book that I am reading, I came up with this:

    @Override
    public void onDraw(Canvas canvas){
        // color the paper
        canvas.drawColor(paperColor);

        // draw ruled lines
        int lineCount = getLineCount();
        Rect r = rect;
        for(int i = 0; i < lineCount; i++){
            int baseLine = getLineBounds(i, r);
            canvas.drawLine(r.left, baseLine+1, r.right, baseLine+1, linePaint);
        }

        // draw margin
        canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);

        // move the text across the margin
        canvas.save();
        canvas.translate(margin, 0);

        // render the text
        super.onDraw(canvas);
        canvas.restore();
    }  

However, though, I only get ruled lines if I add more lines of text to the EditText. This is because getLineCount() has been used. Basically, I want the EditText to look like a page from a notebook.

What modification to the code is needed?

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221

1 Answers1

1

Instead of using getLineCount(), maybe you could do something like this:

private int getDrawableLineCount() {
   return getMeasuredHeight() % getLineHeight();
}

The line height may need adjusting, but that should get you started.

Brian
  • 1,436
  • 8
  • 18