If you don't mind the underline having the same colour as the text in the EditText
, you should really just use the built-in UnderlineSpan
, either by creating it yourself or indirectly through Html.fromHtml(...)
.
private void createUnderlinedText() {
String text = "I am underlined text
Line #2
Line #3
Line #4
Line #5";
EditText underlineSpanEditText = (EditText) findViewById(R.id.underlinespan_edittext);
SpannableStringBuilder sb = new SpannableStringBuilder(text);
sb.setSpan(new UnderlineSpan(), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
underlineSpanEditText.setText(sb);
EditText htmlUnderlineEditText = (EditText) findViewById(R.id.html_underline_edittext);
String html = "<u>I am underlined text</br>Line #2</br>Line #3</br>Line #4</br>Line #5</u>";
htmlUnderlineEditText.setText(Html.fromHtml(html));
}
The main difference with your current approach is that this will only underline the actual text, and not the whole text line. For example, if you run my code snippet, you will find that the underline does not extend to the end of the line when it's broken off by the
or <br/>
. However, depending on the behaviour your after, this may not be what you're looking for.
Edit: So if I understand you correctly, you basically want to keep drawing horizontal lines in your EditText
, no matter wether there is text or not? The 'underline' part in your question was kind of misleading, since, as it turns out, that has little to do with it (in the traditional meaning of the word :)).
Anyways, you can't use getLineCount()
since that will always return the number of lines that contain actual text. That would mean you would have to 'fill' any remaing space with new line characters to get the desired effect, which sounds kind of yucky... A better alternative is probably to base the drawing of horizontal lines on the total height of the EditText
. A quick example, which you can obviously tweak to your own liking:
public class LinedEditText extends EditText {
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.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(left+paddingLeft, baseline, right-paddingRight, baseline, mPaint);
}
super.onDraw(canvas);
}
}
The result looks like this: