Last Updated: February 25, 2016
·
883
· ktusznio

Change text color of a substring

TextView textView = (TextView) view.findViewById(R.id.textView);
String text = getString(R.string.foo);
textView.setText(text, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) textView.getText();
String substring = getString(R.string.substring);
int start = text.indexOf(substring);
int end = start + substring.length();
int red = getResources().getColor(R.color.red);
spannable.setSpan(new ForegroundColorSpan(red), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);