Use a Spannablestring and apply a Replacementspan:
public class ZigZagUnderlineSpan extends ReplacementSpan{
private Paint linePaint;
private int textMessureWidth;
private float lineOffsetFromTextBottom = 0;
private float lineSpaceX = 6;
private float lineSpaceY = 6;
public ZigZagUnderlineSpan(){
linePaint = new Paint();
linePaint.setColor(Color.RED);
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setStrokeWidth(2);
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
textMessureWidth = (int)paint.measureText(text, start, end);
Paint.FontMetricsInt metrics = paint.getFontMetricsInt();
if (fm != null) {
fm.ascent = metrics.ascent;
fm.descent = metrics.descent;
fm.top = metrics.top;
fm.bottom = metrics.bottom;
}
return textMessureWidth;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
canvas.drawText(text, start, end, x, y, paint);
float lineY = y + lineOffsetFromTextBottom;
float xLeft = paint.measureText(text, 0, start);
Path path = new Path();
path.moveTo(xLeft, lineY);
float vertexX = xLeft;
float vertexY = lineY;
for(int n = 0; n < textMessureWidth / lineSpaceX; n++){
path.lineTo(vertexX, vertexY);
vertexX += lineSpaceX;
if (n % 2 == 0) {
vertexY = lineY + lineSpaceY;
}else{
vertexY = lineY;
}
}
path.lineTo(textMessureWidth, vertexY);
canvas.drawPath(path, linePaint);
}
}
Use like this:
SpannableString spannableString = new SpannableString("Texto errado");
spannableString.setSpan(new ZigZagUnderlineSpan(),0,spannableString.length(),SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
What I want is that the
meu app
that detects the error and changes the color of the underline that is in the word.– AndronCreation
@Androncreation you said:
ou uma palavra que esteja errada
, That’s exactly what Spell Check does. You even tried?– Guilherme Nascimento
@Guilhermenascimento wants to do something like a code editor with "Intellisense".
– itscorey
@itscorey then the description of the question this leading to another understanding, that would be a problem called XY problem already much debated in the Meta, the author asks Y when the intention was to ask Y, then I answered what was asked and the problem has to be solved by editing the question and making clear the real need. Still can’t say if it is Intellisense in fact what he wants, so I’ll wait for the AP to review the question, because we can’t base it on hypotheses.
– Guilherme Nascimento
That’s even though I don’t know what you’re thinking
– AndronCreation
@Androncreation but then the intention is not to point out wrong words? Would that be then
– Guilherme Nascimento
I edited the question.
– AndronCreation
@Androncreation ok, as soon as possible I will edit the answer, I already have the notion of how to do what you want, anyway I highly recommend that you read this https://pt.meta.stackoverflow.com/q/499/3635 to avoid future problems like this. Thank you.
– Guilherme Nascimento
It took me a while to understand it too. But I realized it was about
Intellisense
or something like that when he said the wordprint
should be posted at some point, meaning it’s like akeyword
in his Edittext. It was hard to understand anyway.– itscorey
@itscorey yes, the question was with "XY" problem: https://pt.meta.stackoverflow.com/q/499/3635, I am trying to create an example with a native lib and will edit the answer
– Guilherme Nascimento