I created simple code for highlight syntax in EditText.
First i created a HashMap to store keywords and colors.
Map<String,Integer> map = new HashMap<>();
map.put("public",Color.CYAN);
map.put("void", Color.BLUE);
map.put("String",Color.RED);
Then I added a TextWatcher for the EditText.
In afterTextChanged method I used following code to set colors to each keyword,
........
@Override
public void afterTextChanged(Editable editable) {
String string = editable.toString();
String[] split = string.split("\\s");
for(int i = 0 ; i < split.length ; i++){
String s = split[i];
if(map.containsKey(s)){
int index = string.indexOf(s);
int color = map.get(s);
editable.setSpan(new ForegroundColorSpan(color),
index,
index + s.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
This code is working, if i type different words, like "public void String", but It's not working when I type same word, "public public public". It only set color to the first word.
How can i make this work ? Thank you.

