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?
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With