EditText underline not aligning properly after new lines

0

I am trying to create a textarea with dotted lines in all lines, but when new lines are added, it's not aligning properly:

ScreenShot

Code

class LinedEditText extends androidx.appcompat.widget.AppCompatEditText {
private Paint mPaint = new Paint();

public LinedEditText(Context context) {
    super(context);
    initPaint();
}

public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    initPaint();
}

public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initPaint();
}

private void initPaint() {
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setPathEffect(new DashPathEffect(new float[] {10f,20f}, 0f));
    mPaint.setColor(0x80000000);
}

@Override protected void onDraw(Canvas canvas) {
    int left = getLeft();
    int right = getRight();
    int paddingTop = getPaddingTop();
    int paddingBottom = getPaddingBottom();
    int paddingLeft = getPaddingLeft();
    int paddingRight = getPaddingRight();
    int height = getHeight();
    int lineHeight = getLineHeight();
    int count = (height-paddingTop-paddingBottom) / lineHeight;

    for (int i = 0; i < count; i++) {
        int baseline = lineHeight * (i+1) + paddingTop;
        canvas.drawLine(paddingLeft, baseline, right-paddingRight, baseline, mPaint);
    }

    super.onDraw(canvas);
}
}

XML

  <com.example.myapp.LinedEditText
    android:id="@+id/editTextTextMultiLine2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:backgroundTint="#FFFFFF"
    android:layout_marginBottom="8dp"
    android:textSize="24sp"
    android:hint="Start typing your items..."
    android:gravity="start|top"
    android:inputType="textMultiLine"
    app:layout_constraintBottom_toTopOf="@+id/button10"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView12"
   />

In my other device with different screensize have no problem. How can i fix this issue.?And some old devices not showing the dotted line, and this not aligning issue is also present.

android-edittext
android-canvas
android-xml
androidx
asked on Stack Overflow Aug 20, 2020 by sony • edited Aug 21, 2020 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0