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:

@OverrideprotectedvoidonDraw(Canvas canvas) {
            intleft= getLeft();
            intright= getRight();
            intpaddingTop= getPaddingTop();
            intpaddingBottom= getPaddingBottom();
            intpaddingLeft= getPaddingLeft();
            intpaddingRight= getPaddingRight();
            finalintheightWithScrollY= getHeight() + getScrollY();
            Log.d("Height Of View: ",String.valueOf(heightWithScrollY));
            intlineHeight= getLineHeight();
            Log.d("LineHeight: ",String.valueOf(lineHeight));
            intcount= (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)
            {
                floatbaseline= 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:

@OverrideprotectedvoidonDraw(Canvas canvas) {
    intleft= getLeft();
    intright= getRight();
    intpaddingLeft= getPaddingLeft();
    intpaddingRight= getPaddingRight();

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

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

    // 2. Draw line for non-existing rowsfinalintheightWithScrollY= getHeight() + getScrollY();
    intlineHeight= getLineHeight();
    intnextBaseline= 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)"