Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From the code you've posted, you're passing new spans to spanStr and asking it to find them. You'll need to have a reference to the instances of those spans that are actually applied. If that's not feasible or you don't want to track spans directly, you can simply call <a href="http://developer.android.com/reference/android/text/SpannableStringBuilder.html#getSpans%28int,%20int,%20java.lang.Class%3CT%3E%29%20%22getSpans">getSpans</a> to get all the spans applied. You can then filter that array for what you want. </p> <p>If you don't care about the spans in particular, you can also just call <a href="http://developer.android.com/reference/android/text/Html.html#toHtml%28android.text.Spanned%29">Html.toHtml(spanStr)</a> to get an HTML tagged version. </p> <p><strong>edit</strong>: to add code example</p> <p>This will grab all applied StyleSpans which is what you want. </p> <pre><code> /* From the Android docs on StyleSpan: "Describes a style in a span. * Note that styles are cumulative -- both bold and italic are set in * separate spans, or if the base is bold and a span calls for italic, * you get bold italic. You can't turn off a style from the base style."*/ StyleSpan[] mSpans = et.getText().getSpans(0, et.length(), StyleSpan.class); </code></pre> <p>Here's a link to the <a href="http://developer.android.com/reference/android/text/style/StyleSpan.html">StyleSpan</a> docs. </p> <p>To pick out the spans you want if you have various spans mixed in to a collection/array, you can use <code>instanceof</code> to figure out what type of spans you've got. This snippet will check if a particular span <code>mSpan</code> is an instance of StyleSpan and then print its start/end indices and flags. The flags are constants that describe how the span ends behave such as: Do they include and apply styling to the text at the start/end indices or only to text input at an index inside the start/end range).</p> <pre><code> if (mSpan instanceof StyleSpan) { int start = et.getSpanStart(mSpan); int end = et.getSpanEnd(mSpan); int flag = et.getSpanFlags(mSpan); Log.i("SpannableString Spans", "Found StyleSpan at:\n" + "Start: " + start + "\n End: " + end + "\n Flag(s): " + flag); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload