Workaround for EditText ignoring lineSpacingMultiplier

0

Due to the bug mentioned in I can not adjust the edittext linespacing and https://issuetracker.google.com/issues/37009353#comment17

I tried the following workaround, by having custom onTextChanged

LinedEditText.java:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

/**
 * Created by yccheok on 24/3/2018.
 */

public class LinedEditText extends android.support.v7.widget.AppCompatEditText {
    public LinedEditText(Context context) {
        super(context);
        initPaint();
    }

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

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

    private void initPaint() {
        mPaint.setStyle(Paint.Style.STROKE);
        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;

        float originalLineHeight = lineHeight / getLineSpacingMultiplier();

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

        super.onDraw(canvas);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        if (lengthBefore != lengthAfter) {
            float add = getLineSpacingExtra();
            float mul = getLineSpacingMultiplier();
            setLineSpacing(0f, 1f);
            setLineSpacing(add, mul);
        }
    }

    private Paint mPaint = new Paint();
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <mypackage.LinedEditText
        android:id="@+id/edit_text"
        android:gravity="top"
        android:padding="12dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:background="@android:color/transparent"
        android:textSize="18sp"
        android:singleLine="false"
        android:lineSpacingMultiplier="2.5"
        android:lineSpacingExtra="0dp"
        android:inputType="textMultiLine" />

</FrameLayout>

However, it still have problem on the cursor. When you press ENTER to move the cursor to next line, the cursor is drawn by ignoring line spacing multiplier. When you start typing, only then the position of cursor will be corrected.

Please see screenshot

Before pressing ENTER

enter image description here

After pressing ENTER (Cursor is ignoring lineSpacingMultiplier)

enter image description here

Start typing (Cursor is considering lineSpacingMultiplier)

enter image description here

This is a pretty annoying bug, and Google is not yet fixing it.

I was wondering, does anyone of you come across a solid workaround? Do you mind to share with us?

android
asked on Stack Overflow Mar 24, 2018 by Cheok Yan Cheng • edited Jun 19, 2019 by double-beep

2 Answers

0

You may try to override onKeyUp() and call setLineSpacing().

answered on Stack Overflow Mar 24, 2018 by mike • edited Jun 19, 2019 by double-beep
0

Override onMeasure in edit and setBounds in cursor drawable.

Reference: https://github.com/hanks-zyh/LineHeightEditText/issues/1#issuecomment-503476003

answered on Stack Overflow Jun 19, 2019 by 杨冀伟 • edited Jun 19, 2019 by double-beep

User contributions licensed under CC BY-SA 3.0