I am making an app that involves coding and I need edittext to recognize if the word typed was 'something' then depending if it is registered to be colored, it will color the word. Here is what I want to do, when the user is typing and types 'function' I want it to automatically highlight. Same goes to any other 'function' word, '()', ' " ', and many other words the user types.
You can accomplish this by using a TextWatcher like so:
editText.addTextChangedListener(new TextWatcher() {
final String FUNCTION = "function";
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
int index = s.toString().indexOf(FUNCTION);
if (index >= 0) {
s.setSpan(
new ForegroundColorSpan(Color.CYAN),
index,
index + FUNCTION.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
});
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