public static void setColorForPath(Spannable spannable, String[] paths, int color) { for (int i = 0; i < paths.length; i++) { int indexOfPath = spannable.toString().indexOf(paths[i]); if (indexOfPath == -1) { return; } spannable.setSpan(new ForegroundColorSpan(color), indexOfPath, indexOfPath + paths[i].length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } }
Использование
Spannable spannable = new SpannableString("Your big island ADVENTURE"); Utils.setColorForPath(spannable, new String[] { "big", "ADVENTURE" }, Color.BLUE); textView.setText(spannable);