sanic
sanic

Reputation: 2085

Save only Bolded Text to a String Array in a Spanned String?

I have paragraph long stories that contain spanned strings with mostly regular text and a few bolded text (1 or 2 words) here and there. It's in an Edittext. I want to be able to search through that spanned string in the editText and save each bolded text to a string. I'm not sure how to do that though. Any suggestions?

Upvotes: 2

Views: 194

Answers (1)

Jason Robinson
Jason Robinson

Reputation: 31294

If you're using SpannableString, you can use the getSpans method:

StyleSpan[] spans = ss.getSpans(0, ss.length(), StyleSpan.class);
List<String> boldedWords = new ArrayList<String>();
for(StyleSpan span : spans) {
    if(span.getStyle() & Typeface.BOLD) {
        int start = ss.getSpanStart(span);
        int end = ss.getSpanEnd(span);
        boldedWords.add(ss.subSequence(start, end).toString());
    }
}

Upvotes: 2

Related Questions