Edittext style texts - Underline in Zigzag

Asked

Viewed 135 times

2

I have an edittext that when it detects a specific word it highlights it for example:

print("Destaque")

The word print will be deployed.

Only so far so good but I want every time it detects a variable or a word that is wrong according to a set of words recognized by my app (a code editor) it does the following:

inserir a descrição da imagem aqui

Explanation: I want to get more or less these underline and the color does not matter I choose it.

I’m making a kind of code editor.

3 answers

1


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);

inserir a descrição da imagem aqui

0

This you wish is called Spell Check, I’m almost certain this should be enabled by default, however may "try to":

 editText.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);

I still believe that maybe you have disabled (or is default) the spellcheck on your Android system (emulator or mobile), so to enable try this (the path may vary depending on the version of Android):

Configure > Language and input then select the checkbox Spell checker:

configurar


Disable spellCheck

One extra tip, in case someone wants to disable is set in the EditText this:

android:inputType="textNoSuggestions"

Or via code:

 editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
  • 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 you said: ou uma palavra que esteja errada, That’s exactly what Spell Check does. You even tried?

  • @Guilhermenascimento wants to do something like a code editor with "Intellisense".

  • @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.

  • That’s even though I don’t know what you’re thinking

  • @Androncreation but then the intention is not to point out wrong words? Would that be then

  • I edited the question.

  • @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.

  • It took me a while to understand it too. But I realized it was about Intellisense or something like that when he said the word print should be posted at some point, meaning it’s like a keyword in his Edittext. It was hard to understand anyway.

  • @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

Show 5 more comments

-1

Use regex with replace, as I’m not sure what kind of editor you use I did a basic example that says "$test" and "var test":

EXAMPLE Regex click here to see

HTML from the example:

<textarea id="texto" rows="10" style="width: 100%;"></textarea>
<div id="editor" style="width:100%; height:800px;background: #eee;"></div>

Js do Exemplo:

$("#editor").click(function() {
  $("#texto").focus();
});
$("#texto").keyup(function() {
  var texto = $("#texto").val();
  var sub = texto.replace(/(\$|(var))/g,'<u style="color: red;">$1</u>');
  $("#editor").html(sub);
});
  • I would like to do but by android

  • Eita was badly kkkk nor did I heed the tags, as I deactivated those related to JAVA I thought the doubt was about javascript...

  • @William Bianardi, don’t worry.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.