Reputation: 363
my Spannable won't change color of repetitive characters .For example here i have two same character of "$" dollar sign ,first one is changed to blue color but second isn't .I am putting text that i wont to change is String array. When i debug i can see all text in it how it's suppose and on proper indexes.
String secondRowString = "Based on your current monthly savings of" + " $" + FormattedValue.formattedValue(model.getRetirementDdata().getCombinedCurrentMonthlySavings())
+ " at a rate of return of " + FormattedValue.formattedValue(model.getRetirementDdata().getAverageRateOfReturn()) + "% "
+ " , you are on track to have " + "$" + FormattedValue.formattedValue(model.getRetirementDdata().getHowMuchIWillHave())
+ " in total savings by " + model.getRetirementDdata().getRetirementMembersList().get(0).getName()
+ " age " + model.getRetirementDdata().getRetirementMembersList().get(0).getAgeOfRetirement();
SpannableString strThirdRow = new SpannableString(secondRowString);
SpannableTextUtils.setColorForPath(strThirdRow, new String[]{"$"
, FormattedValue.formattedValue(model.getRetirementDdata().getCombinedCurrentMonthlySavings())
, FormattedValue.formattedValue(model.getRetirementDdata().getAverageRateOfReturn()), "%", "$"
, FormattedValue.formattedValue(model.getRetirementDdata().getHowMuchIWillHave())
, String.valueOf(model.getRetirementDdata().getRetirementMembersList().get(0).getAgeOfRetirement())}, ContextCompat.getColor(context, R.color.debt_payments));
binding.secondTV.setText(strThirdRow);
and my SpannableString.setColorsForPath()
public static void setColorForPath(Spannable spannable, String[] paths, int color) {
for (String path : paths) {
int indexOfPath = spannable.toString().indexOf(path);
if (indexOfPath == -1) {
continue;
}
spannable.setSpan(new ForegroundColorSpan(color), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
Upvotes: 1
Views: 45
Reputation: 21043
Its a logical bug in your code indexOf
will return the first occurrence Each time . While you need to find all the occurrence of a word in String .
This can be done in multiple ways . Basically you need to solve problem Find Indexes of all occurrences of a word in a string
.
You can use below approach .
public static void setColorForPath(Spannable spannable, String[] paths, int color) {
for (String path : paths) {
int indexOfPath = spannable.toString().indexOf(path);
while(indexOfPath >= 0) {
spannable.setSpan(new ForegroundColorSpan(color), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), indexOfPath,
indexOfPath + path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
indexOfPath = spannable.toString().indexOf(path, indexOfPath+1);
}
}
}
Upvotes: 1