public static CharSequence setSpan(final CharSequence cs, final Object span, final int start, final int end, final int priority) {
        if (cs == null || cs.length() == 0 || span == null) {
            return cs;
        }
        final Spannable sp;
        if (cs instanceof Spannable) {
            sp = (Spannable) cs;
        } else {
            sp = new SpannableString(cs);
        }
        final int startToUse = Math.min(cs.length(), Math.max(0, start));
        final int endToUse = end < 0 ? cs.length() : Math.min(cs.length(), Math.max(end, startToUse));
        if (endToUse <= startToUse) {
            return cs;
        }

        int spanFlags = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
        spanFlags |= (Math.min(255, Math.max(0, priority)) << Spannable.SPAN_PRIORITY_SHIFT) & Spannable.SPAN_PRIORITY;
        sp.setSpan(span, startToUse, endToUse, spanFlags);
        return sp;
    }
Convenience method to apply a span to a CharSequence. See {@link Spannable#setSpan(Object, int, int, int)} for details.

@param cs       CharSequence to apply span to
@param span     the span to use. Usually a class implementing {@link android.text.style.CharacterStyle}, e.g. {@link ForegroundColorSpan}
@param start    start index to add span. if <0 then 0 is used
@param end      end index to add span. if <0 then cs.length() is used. if end<start, then no span is applied
@param priority span priority. Values > 255 will be set to 255
@return CharSequence with span applied
main/src/main/java/cgeo/geocaching/utils/TextUtils.java