Skip to content Skip to sidebar Skip to footer

Incorrect EditText Line Spacing Behavior In Target API 28, When Dealing With Non-English Unicode (Like Chinese, Japanese)

We notice that, during targetSdkVersion 28, EditText will tend to 'slightly push down' the line after input, when non-English unicode (Like Chinese, Japanese, ...) is being entered

Solution 1:

Well i managed to do it in the following manner. My onDraw function:

@Override
        protected void onDraw(Canvas canvas) {
            int left = getLeft();
            int right = getRight();
            int paddingTop = getPaddingTop();
            int paddingBottom = getPaddingBottom();
            int paddingLeft = getPaddingLeft();
            int paddingRight = getPaddingRight();
            final int heightWithScrollY = getHeight() + getScrollY();
            Log.d("Height Of View: ",String.valueOf(heightWithScrollY));
            int lineHeight = getLineHeight();
            Log.d("LineHeight: ",String.valueOf(lineHeight));
            int count = (heightWithScrollY-paddingTop-paddingBottom) / lineHeight;
            Log.d("Count: ",String.valueOf(count));
            mPaint.setColor(noteLineColor);
            mPaint.setTypeface(this.getTypeface());
            Log.d("Descent: ",String.valueOf(mPaint.descent()));

            for(int i=lineHeight;i<=count*lineHeight;i+=lineHeight)
            {
                float baseline = i + paddingTop + mPaint.descent();
                canvas.drawLine(left+paddingLeft,baseline,right-paddingRight,baseline,mPaint);
                Log.d("XYXY:",String.valueOf(left+paddingLeft)+" "+String.valueOf(baseline)+" "+String.valueOf(right-paddingRight));
            }
            super.onDraw(canvas);
        }  

and i used view as

 <com.yocto.wenote.note.LinedEditText
        android:id="@+id/body_edit_text"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:gravity="top"
        android:layout_marginBottom="12dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:textSize="18sp"
        android:singleLine="false"
        android:lineSpacingMultiplier="1.4"
        android:inputType="textMultiLine|textCapSentences"/>

And last but not the least i used this implementation in my build.gradle dependencies (I personally think using this alpha05 version helped it to solve the issue but i have not checked otherwise)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}  

Solution 2:

You have to query the baseline for each existing row to draw your line in the correct position. I simplified your code, this version will fix your problem, I tested on Android P and Android Q:

@Override
protected void onDraw(Canvas canvas) {
    int left = getLeft();
    int right = getRight();
    int paddingLeft = getPaddingLeft();
    int paddingRight = getPaddingRight();

    mPaint.setColor(noteLineColor);
    mPaint.setTypeface(this.getTypeface());

    Layout layout = getLayout();
    // 1. Draw line for existing rows
    for (int i = 0; i < layout.getLineCount(); i++) {
        int baseline = layout.getLineBaseline(i);
        canvas.drawLine(
                left + paddingLeft,
                baseline,
                right - paddingRight,
                baseline,
                mPaint
        );
    }

    // 2. Draw line for non-existing rows
    final int heightWithScrollY = getHeight() + getScrollY();
    int lineHeight = getLineHeight();
    int nextBaseline = layout.getHeight() + lineHeight;
    while(nextBaseline < heightWithScrollY){
        canvas.drawLine(
                left + paddingLeft,
                nextBaseline,
                right - paddingRight,
                nextBaseline,
                mPaint
        );
        nextBaseline += lineHeight;
    }

    super.onDraw(canvas);
}

Post a Comment for "Incorrect EditText Line Spacing Behavior In Target API 28, When Dealing With Non-English Unicode (Like Chinese, Japanese)"