Change distance between EditText lines and wrt cursor position

I am trying to create an EditText so that the space between each line should be like 20 dp . See image below.

If I use extra spacing, the cursor position is not aligned correctly with the line. I have reached the distance between the lines, but the cursor is not aligned with the line. See the red box highlighted in the image below.

 package com.example.dev_task_197_keyboard_accesory; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.widget.EditText; public class LineEditText extends EditText { private Rect mRect; private Paint mPaint; // we need this constructor for LayoutInflater public LineEditText(Context context, AttributeSet attrs) { super(context, attrs); mRect = new Rect(); mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL_AND_STROKE); mPaint.setColor(Color.BLUE); //SET YOUR OWN COLOR HERE setMinLines(15); } @Override protected void onDraw(Canvas canvas) { int height = getHeight(); int line_height = getLineHeight(); int count = height / line_height; if(getLineCount() > count){ count = getLineCount(); } Rect r = mRect; Paint paint = mPaint; int baseline = getLineBounds(0, r); for (int i = 0; i < count; i++) { canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); baseline += getLineHeight();//next line } // Finishes up by calling the parent method super.onDraw(canvas); } } 

For line spacing, add the lineSpaceExtra and lineSpaceMultiplier to the xml .

Please offer.

enter image description here

+7
android android-edittext
source share
1 answer

Here you can find two possible solutions to your problem. I tested the first one (it works only with API 12+), it worked for me.

+1
source share

All Articles