How to make EditText with syntax highlighting? There are ready solutions? HTML tags to insert as it seems to me, is not the best option, the app will slow down.
Asked
Active
Viewed 2,399 times
3
-
2Check this http://stackoverflow.com/questions/11987660/android-syntax-highlighting – Zied R. Jul 19 '16 at 07:14
2 Answers
7
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);
}
}
});
Refered from

Community
- 1
- 1

Dishant Anand
- 362
- 1
- 14
0
Can you use like this .,
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);
}
}
});
please try this , this may be helpful .

sankar ganesh
- 133
- 1
- 8