Expands a selection to include spans - Android android.text

Android examples for android.text:SpannableString

Description

Expands a selection to include spans

Demo Code

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.text.Editable;
import android.text.SpannedString;
import android.util.Pair;

public class Main{

    /**/*  w  w w. j a  v  a  2  s  . com*/
     * Expands a selection to include spans
     * @param text
     * @param start
     * @param end
     * @return the pair of start and end values
     */
    public static Pair<Integer, Integer> expandSelectionForSpans(
            Editable text, int start, int end) {
        // make sure we don't cut any spans in half
        SpannedString[] spans = text.getSpans(start, end,
                SpannedString.class);
        for (SpannedString s : spans) {
            int spanStart = text.getSpanStart(s);
            int spanEnd = text.getSpanEnd(s);
            if (spanStart < start) {
                start = spanStart;
            }
            if (spanEnd > end) {
                end = spanEnd;
            }
        }
        return new Pair(start, end);
    }

}

Related Tutorials