I want to highlight the line with the cursor on it in an EditText. I have a semi-transparent View which will take place over the line. I need to get a character coordinates on screen in an EditText. (the first and last one of the line) The best I found was getting a text dimensions, but this can't be used for what I want. I tried nothing, because I have no idea where to begin.
Is this possible? If not how can I do it?
This can be rather easily accomplished by subclassing EditText
, and inserting the highlight effect in its onDraw()
method, before everything else is drawn. EditText
's Layout
object can give us the line number from the current cursor position, with which we can get the bounds as a Rect
. We then draw the highlight rectangle, and call the super method to draw the rest of the View
.
For example:
public class HighlightEditText extends EditText {
private static final int HIGHLIGHTER_YELLOW = 0x88f3f315;
private Rect lineBounds;
private Paint highlightPaint;
private int lineNumber;
private boolean lineHighlightEnabled = true;
public HighlightEditText(Context context) {
this(context, null);
}
public HighlightEditText(Context context, AttributeSet a) {
super(context, a);
lineBounds = new Rect();
highlightPaint = new Paint();
highlightPaint.setColor(HIGHLIGHTER_YELLOW);
}
@Override
protected void onDraw(Canvas canvas) {
if (lineHighlightEnabled) {
lineNumber = getLayout().getLineForOffset(getSelectionStart());
getLineBounds(lineNumber, lineBounds);
canvas.drawRect(lineBounds, highlightPaint);
}
super.onDraw(canvas);
}
public void setLineHighlightEnabled(boolean enabled) {
lineHighlightEnabled = enabled;
invalidate();
}
public boolean isLineHighlightEnabled() {
return lineHighlightEnabled;
}
public void setLineHighlightColor(int color) {
highlightPaint.setColor(color);
if (lineHighlightEnabled) {
invalidate();
}
}
public int getLineHighlightColor() {
return highlightPaint.getColor();
}
}
You can include this custom class in your layouts as usual. For example:
<com.mycompany.myapp.HighlightEditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|left"
android:imeOptions="flagNoExtractUi" />
Please note that if you're using the AppCompat library and its amenities, you should instead extends AppCompatEditText
, to ensure the tinting and whatnot are handled appropriately.