0

I use this piece of code to draw lines across the whole screen which is works well, but when I scroll it is slow when scrolling.

public  class LinedEditText extends androidx.appcompat.widget.AppCompatEditText{

private Rect mRect;
protected Paint mPaint;

// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setColor(Color.parseColor("#82B6C6"));
}

@Override
public void onDraw(Canvas canvas) {
    //int count = getLineCount();

    int height = getHeight();
    int line_height = getLineHeight();

    int count = height / line_height;

    if (getLineCount() > count)
        count = getLineCount();//for long text with scrolling

    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);//first line

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }

    super.onDraw(canvas);
}

}

Here is the XML code for edit text

<com.example.myapp.LinedEditText
        android:id="@+id/et_note"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:autofillHints="emailAddress"
        android:background="@null"
        android:gravity="start"
        android:hint="Add note"
        android:inputType="textCapSentences|textMultiLine"
        android:padding="10dp"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

As I said it is slow during scrolling, so for this reason I have to use ScrollView, but after surround the EditText with scrollView, the lines are lines are removed from the EditText except the first line. When the text goes to new line, the EditText automatically create a new line which is not what I want, I want the whole screen with lines. How to solve that?

neverStop
  • 43
  • 5

1 Answers1

0

I found the answer on StackOverFlow, all I need to do is

android:fillViewport="true"

then worked perfectly. source

neverStop
  • 43
  • 5