Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text color as the user is typing?

While the user is typing in an EditText I would like certain keywords to change colors. I could place HTML tags around the keywords when the app discovers the user typed it and use Html.fromHtml but the user will be entering real HTML tags themselves and that would mess it up. So I guess I should use Spannable?

And how exactly should I scan the EditText for these keywords? Meaning - what is the most efficient way? I was thinking maybe an array of all the keywords - and then loop through and see if any matches are found . Any ideas on how to approach this?

like image 478
bwoogie Avatar asked Nov 04 '22 12:11

bwoogie


1 Answers

Hi Use TextWatcher on EditText to see what user is typing and use Linkify to check pattern. https://developer.android.com/reference/android/text/util/Linkify.html

EditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

}

Use onTextChanged Listener to check what user is typing.

Sample for Pattern matching.. int flags = Pattern.CASE_INSENSITIVE; Pattern p = Pattern.compile("\[0-9]*\", flags); Linkify.addLinks(myTextView, p, "content://com.foo");

like image 108
om252345 Avatar answered Nov 13 '22 02:11

om252345