티스토리 뷰

 

 

 

 

 

 

 

 

dataText 는 필터 걸기 전 전체 텍스트,

searchText 는 강조하고 싶은 텍스트

 

 

public SpannableStringBuilder spannableStringBuilder(String dataText, String searchText) {
SpannableStringBuilder sp = new SpannableStringBuilder(dataText);
List<data> data = new ArrayList<>();
for (int i=0; i<dataText.length(); i++) {
String b;
try {
b = dataText.substring(i,i+searchText.length());
}catch (Exception e) {
continue;
}
if (b.equals(searchText)) {
data.add(new data(i,searchText.length()));
}
}

for (int i=0; i<data.size(); i++) {
sp.setSpan(new ForegroundColorSpan(Color.RED), data.get(i).location,data.get(i).location + data.get(i).count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

return sp;
}

class data {
int location;
int count;

public data(int location, int count) {
this.location = location;
this.count = count;
}
}