how to set underline in a sentence by button sequence?

57 Views Asked by At

I cannot get the method for this but how can I set/modified a String in a sentence with an underline in a textview? i wanted like this, when i click the button the underline will move to the next string and click again the underline will move again. I really don't know what type of code or what kind of method is using for like this or what ever call for this.

example 1: _the_ red fox jumps over the lazy dog.

if the button is click then the underline should be in "the".

example 2: the _red_ fox jumps over the lazy dog.

if the button is click again then the underline should be in "red".

and so on.

I am new in this type of method i cannot search about this kind of code. Please help me for this kind of code Thank you.

UPDATE:

how can i set a underline code for like this:

The _***_ *** ***** **** *** **** ***.

and when i click again the button the under move again like this:

The *** _***_ ***** **** *** **** ***.
1

There are 1 best solutions below

3
On

You should use UnderlineSpan Try this

    public CharSequence formatText(String base, String highlight) {
        int start = base.indexOf(highlight);
        if (start >= 0) {
            SpannableString span = new SpannableString(base);
            span.setSpan(new UnderlineSpan(), start, start + highlight.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return span;
        }
        return base;
    }

When you click you should pass base string and highlight string you want

textView.setText(formatText("the red fox jumps over the lazy dog.", "the"));