Java Code Examples for android.text.Spannable#length()

The following examples show how to use android.text.Spannable#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SuggestionView.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private boolean applyHighlightToMatchRegions(
        Spannable str, List<MatchClassification> classifications) {
    boolean hasMatch = false;
    for (int i = 0; i < classifications.size(); i++) {
        MatchClassification classification = classifications.get(i);
        if ((classification.style & MatchClassificationStyle.MATCH)
                == MatchClassificationStyle.MATCH) {
            int matchStartIndex = classification.offset;
            int matchEndIndex;
            if (i == classifications.size() - 1) {
                matchEndIndex = str.length();
            } else {
                matchEndIndex = classifications.get(i + 1).offset;
            }
            matchStartIndex = Math.min(matchStartIndex, str.length());
            matchEndIndex = Math.min(matchEndIndex, str.length());

            hasMatch = true;
            // Bold the part of the URL that matches the user query.
            str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
                    matchStartIndex, matchEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return hasMatch;
}
 
Example 2
Source File: Selection.java    From JotaTextEditor with Apache License 2.0 6 votes vote down vote up
/**
 * Move the selection end to the buffer offset physically below
 * the current selection end.
 */
public static boolean extendDown(Spannable text, Layout layout) {
    int end = getSelectionEnd(text);
    int line = layout.getLineForOffset(end);

    if (line < layout.getLineCount() - 1) {
        int move;

        if (layout.getParagraphDirection(line) ==
            layout.getParagraphDirection(line + 1)) {
            float h = layout.getPrimaryHorizontal(end);
            move = layout.getOffsetForHorizontal(line + 1, h);
        } else {
            move = layout.getLineStart(line + 1);
        }

        extendSelection(text, move);
        return true;
    } else if (end != text.length()) {
        extendSelection(text, text.length());
        return true;
    }

    return true;
}
 
Example 3
Source File: DNSProxyActivity.java    From personaldnsfilter with GNU General Public License v2.0 6 votes vote down vote up
private String getSelectedText(boolean fullLine){

		int start= logOutView.getSelectionStart();
		int end = logOutView.getSelectionEnd();
		String selection = "";
		if (end > start) {
			Spannable text = logOutView.getText();
			if (fullLine) {
				while (text.charAt(start) != '\n' && start > 0)
					start--;
				if (start !=0)
					start++;
				while (end < text.length()-1 && text.charAt(end) != '\n')
					end++;

				logOutView.setSelection(start, end);
			}
			selection = text.subSequence(start, end).toString();
		}
		return selection;
	}
 
Example 4
Source File: RTManager.java    From memoir with Apache License 2.0 5 votes vote down vote up
private String getLinkText(RTEditText editor, RTSpan<String> span) {
    Spannable text = editor.getText();
    final int spanStart = text.getSpanStart(span);
    final int spanEnd = text.getSpanEnd(span);
    String linkText = null;
    if (spanStart >= 0 && spanEnd >= 0 && spanEnd <= text.length()) {
        linkText = text.subSequence(spanStart, spanEnd).toString();
        mLinkSelection = new Selection(spanStart, spanEnd);
    } else {
        mLinkSelection = editor.getSelection();
    }
    return linkText;
}
 
Example 5
Source File: Selection.java    From JotaTextEditor with Apache License 2.0 5 votes vote down vote up
/**
 * Move the cursor to the buffer offset physically below the current
 * offset, or return false if the cursor is already on the bottom line.
 */
public static boolean moveDown(Spannable text, Layout layout) {
    int start = getSelectionStart(text);
    int end = getSelectionEnd(text);

    if (start != end) {
        int min = Math.min(start, end);
        int max = Math.max(start, end);

        setSelection(text, max);

        if (min == 0 && max == text.length()) {
            return false;
        }

        return true;
    } else {
        int line = layout.getLineForOffset(end);

        if (line < layout.getLineCount() - 1) {
            int move;

            if (layout.getParagraphDirection(line) ==
                layout.getParagraphDirection(line + 1)) {
                float h = layout.getPrimaryHorizontal(end);
                move = layout.getOffsetForHorizontal(line + 1, h);
            } else {
                move = layout.getLineStart(line + 1);
            }

            setSelection(text, move);
            return true;
        }
    }

    return mDisableLostFocus;
}
 
Example 6
Source File: BrailleIME.java    From brailleback with Apache License 2.0 5 votes vote down vote up
private void addMarkingSpan(Spannable spannable, MarkingSpan span,
        int position) {
    if (position < spannable.length()) {
        spannable.setSpan(span, position, spannable.length(),
           Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
 
Example 7
Source File: FindActionModeCallback.java    From aard2-android with GNU General Public License v3.0 5 votes vote down vote up
void setText(String text) {
    editText.setText(text);
    Spannable span = (Spannable) editText.getText();
    int length = span.length();
    // Ideally, we would like to set the selection to the whole field,
    // but this brings up the Text selection CAB, which dismisses this
    // one.
    Selection.setSelection(span, length, length);
    // Necessary each time we set the text, so that this will watch
    // changes to it.
    span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
 
Example 8
Source File: Selection.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Move the cursor to the buffer offset physically below the current
 * offset, or return false if the cursor is already on the bottom line.
 */
public static boolean moveDown(Spannable text, Layout layout) {
    int start = getSelectionStart(text);
    int end = getSelectionEnd(text);

    if (start != end) {
        int min = Math.min(start, end);
        int max = Math.max(start, end);

        setSelection(text, max);

        if (min == 0 && max == text.length()) {
            return false;
        }

        return true;
    } else {
        int line = layout.getLineForOffset(end);

        if (line < layout.getLineCount() - 1) {
            int move;

            if (layout.getParagraphDirection(line) ==
                layout.getParagraphDirection(line + 1)) {
                float h = layout.getPrimaryHorizontal(end);
                move = layout.getOffsetForHorizontal(line + 1, h);
            } else {
                move = layout.getLineStart(line + 1);
            }

            setSelection(text, move);
            return true;
        }
    }

    return mDisableLostFocus;
}
 
Example 9
Source File: Effect.java    From memoir with Apache License 2.0 5 votes vote down vote up
/**
 * Remove all effects of this type from the currently selected text of the active RTEditText.
 * If the selection is empty (cursor), formatting for the whole text is removed.
 */
public final void clearFormattingInSelection(RTEditText editor) {
    Spannable text = editor.getText();

    // if no selection --> select the whole text
    // otherwise use the getSelection method (implented by sub classes)
    Selection selection = new Selection(editor);
    selection = selection.isEmpty() ? new Selection(0, text.length()) : getSelection(editor);

    List<RTSpan<V>> spans = getSpans(text, selection, SpanCollectMode.EXACT);
    for (Object span : spans) {
        editor.getText().removeSpan(span);
    }
}
 
Example 10
Source File: UIUtil.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void colorizeSpannableGreen(String prependString, Spannable s, Context context) {
    if (context == null) {
        return;
    }
    int offset = prependString.length();
    if (s.length() > 0) {
        s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.green_light)), 0, s.length() > 10 ? 11 + offset : s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (s.length() > 58) {
            s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.white_90)), 11 + offset, 58 + offset, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.green_light)), 58 + offset, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 11
Source File: Html.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
private static void setSpanFromMark(Spannable text, Object mark, Object... spans) {
    int where = text.getSpanStart(mark);
    text.removeSpan(mark);
    int len = text.length();
    if (where != len) {
        for (Object span : spans) {
            text.setSpan(span, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 12
Source File: UIUtil.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void colorizeSpannableBright(String prependString, Spannable s, Context context) {
    if (context == null) {
        return;
    }
    int offset = prependString.length();
    if (s.length() > 0) {
        s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.ltblue)), 0, s.length() > 10 ? 11 + offset : s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (s.length() > 58) {
            s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.ltblue)), 58 + offset, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 13
Source File: HtmlToSpannedConverter.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
private void setSpanFromMark(String tag, Spannable text, Object mark, Object... spans) {
    int where = text.getSpanStart(mark);
    text.removeSpan(mark);
    int len = text.length();
    if (where != len) {
        for (Object span : spans) {
            if (mSpanCallback != null) {
                span = mSpanCallback.onSpanCreated(tag, span);
            }
            text.setSpan(span, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 14
Source File: UIUtil.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Colorize a string in the following manner:
 * First 11 characters are blue transparent
 * Last 6 characters are blue transparent
 *
 * @param s       Spannable
 * @param context Context
 */
public static void colorizeSpannable(Spannable s, Context context) {
    if (context == null) {
        return;
    }
    if (s.length() > 0) {
        s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.ltblue_dark_transparent)), 0, s.length() > 10 ? 11 : s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (s.length() > 58) {
            s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.ltblue_dark_transparent)), 58, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 15
Source File: DNSProxyActivity.java    From personaldnsfilter with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void run() {

	if (!scroll_locked) {
		m_logStr = m_logStr.replace("FILTERED:",IN_FILTER_PREF);
		m_logStr = m_logStr.replace("ALLOWED:",NO_FILTER_PREF);

		addToLogView(m_logStr);

		int logSize = logOutView.getText().length();

		if (logSize >= 10000) {
			Spannable logStr = logOutView.getText();
			int start = logSize / 2;

			while (logStr.charAt(start) != '\n' && start < logStr.length()-1)
				start++;

			logOutView.setText(logStr.subSequence(start, logStr.length()));

		}

		if (!advancedConfigCheck.isChecked()) { //avoid focus lost when editing advanced settings
			logOutView.setSelection(logOutView.getText().length());
			scrollView.fullScroll(ScrollView.FOCUS_DOWN);
		}
	}
	String version = "<unknown>";
	String connCnt = "-1";
	String lastDNS = "<unknown>";
	try {
		version = CONFIG.getVersion();
		connCnt = CONFIG.openConnectionsCount()+"";
		lastDNS= CONFIG.getLastDNSAddress();
	} catch (IOException e){
		addToLogView(e.toString()+"\n");
	}
	setTitle("personalDNSfilter V" + version + " (Connections:" + connCnt + ")");
	dnsField.setText(lastDNS);
}
 
Example 16
Source File: Html.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
private static void setSpanFromMark(Spannable text, Object mark, Object... spans) {
    int where = text.getSpanStart(mark);
    text.removeSpan(mark);
    int len = text.length();
    if (where != len) {
        for (Object span : spans) {
            text.setSpan(span, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 17
Source File: CharSequenceUtils.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public static List<CharSequence> split(Spannable charSequence, char c) {
    List<CharSequence> out = new ArrayList<>();
    int begin = 0;
    for (int i = 0; i < charSequence.length(); ++i) {
        if (charSequence.charAt(i) == c) {
            out.add(StylingHelper.subSequence(charSequence, begin, i));
            begin = ++i;
        }
    }
    if (begin < charSequence.length()) {
        out.add(StylingHelper.subSequence(charSequence, begin, charSequence.length()));
    }
    return out;
}
 
Example 18
Source File: LongClickableLinkMovementMethod.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
private boolean action(int what, TextView widget, Spannable buffer) {
    Layout layout = widget.getLayout();

    int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
    int areatop = widget.getScrollY();
    int areabot = areatop + widget.getHeight() - padding;

    int linetop = layout.getLineForVertical(areatop);
    int linebot = layout.getLineForVertical(areabot);

    int first = layout.getLineStart(linetop);
    int last = layout.getLineEnd(linebot);

    MyURLSpan[] candidates = buffer.getSpans(first, last, MyURLSpan.class);

    int a = Selection.getSelectionStart(buffer);
    int b = Selection.getSelectionEnd(buffer);

    int selStart = Math.min(a, b);
    int selEnd = Math.max(a, b);

    if (selStart < 0) {
        if (buffer.getSpanStart(FROM_BELOW) >= 0) {
            selStart = selEnd = buffer.length();
        }
    }

    if (selStart > last) {
        selStart = selEnd = Integer.MAX_VALUE;
    }
    if (selEnd < first) {
        selStart = selEnd = -1;
    }

    switch (what) {
        case CLICK:
            if (selStart == selEnd) {
                return false;
            }

            MyURLSpan[] link = buffer.getSpans(selStart, selEnd, MyURLSpan.class);

            if (link.length != 1) {
                return false;
            }

            link[0].onClick(widget);
            break;

        case UP:
            int best_start = -1;
            int best_end = -1;

            for (MyURLSpan candidate1 : candidates) {
                int end = buffer.getSpanEnd(candidate1);

                if (end < selEnd || selStart == selEnd) {
                    if (end > best_end) {
                        best_start = buffer.getSpanStart(candidate1);
                        best_end = end;
                    }
                }
            }

            if (best_start >= 0) {
                Selection.setSelection(buffer, best_end, best_start);
                return true;
            }

            break;

        case DOWN:
            best_start = Integer.MAX_VALUE;
            best_end = Integer.MAX_VALUE;

            for (MyURLSpan candidate : candidates) {
                int start = buffer.getSpanStart(candidate);

                if (start > selStart || selStart == selEnd) {
                    if (start < best_start) {
                        best_start = start;
                        best_end = buffer.getSpanEnd(candidate);
                    }
                }
            }

            if (best_end < Integer.MAX_VALUE) {
                Selection.setSelection(buffer, best_start, best_end);
                return true;
            }

            break;
    }

    return false;
}
 
Example 19
Source File: CountLinkMovementMethod.java    From timecat with Apache License 2.0 4 votes vote down vote up
private boolean action(int what, TextView widget, Spannable buffer) {
    Layout layout = widget.getLayout();

    int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
    int areatop = widget.getScrollY();
    int areabot = areatop + widget.getHeight() - padding;

    int linetop = layout.getLineForVertical(areatop);
    int linebot = layout.getLineForVertical(areabot);

    int first = layout.getLineStart(linetop);
    int last = layout.getLineEnd(linebot);

    ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);

    int a = Selection.getSelectionStart(buffer);
    int b = Selection.getSelectionEnd(buffer);

    int selStart = Math.min(a, b);
    int selEnd = Math.max(a, b);

    if (selStart < 0) {
        if (buffer.getSpanStart(FROM_BELOW) >= 0) {
            selStart = selEnd = buffer.length();
        }
    }

    if (selStart > last) selStart = selEnd = Integer.MAX_VALUE;
    if (selEnd < first) selStart = selEnd = -1;

    switch (what) {
        case CLICK:
            if (selStart == selEnd) {
                return false;
            }

            ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);

            if (link.length != 1) return false;

            link[0].onClick(widget);
            break;

        case UP:
            int beststart, bestend;

            beststart = -1;
            bestend = -1;

            for (int i = 0; i < candidates.length; i++) {
                int end = buffer.getSpanEnd(candidates[i]);

                if (end < selEnd || selStart == selEnd) {
                    if (end > bestend) {
                        beststart = buffer.getSpanStart(candidates[i]);
                        bestend = end;
                    }
                }
            }

            if (beststart >= 0) {
                Selection.setSelection(buffer, bestend, beststart);
                return true;
            }

            break;

        case DOWN:
            beststart = Integer.MAX_VALUE;
            bestend = Integer.MAX_VALUE;

            for (int i = 0; i < candidates.length; i++) {
                int start = buffer.getSpanStart(candidates[i]);

                if (start > selStart || selStart == selEnd) {
                    if (start < beststart) {
                        beststart = start;
                        bestend = buffer.getSpanEnd(candidates[i]);
                    }
                }
            }

            if (bestend < Integer.MAX_VALUE) {
                Selection.setSelection(buffer, beststart, bestend);
                return true;
            }

            break;
    }

    return false;
}
 
Example 20
Source File: MyLinkMovementMethod.java    From FoldText_Java with MIT License 4 votes vote down vote up
private boolean action(int what, TextView widget, Spannable buffer) {
    Layout layout = widget.getLayout();

    int padding = widget.getTotalPaddingTop() +
                  widget.getTotalPaddingBottom();
    int areaTop = widget.getScrollY();
    int areaBot = areaTop + widget.getHeight() - padding;

    int lineTop = layout.getLineForVertical(areaTop);
    int lineBot = layout.getLineForVertical(areaBot);

    int first = layout.getLineStart(lineTop);
    int last = layout.getLineEnd(lineBot);

    ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);

    int a = Selection.getSelectionStart(buffer);
    int b = Selection.getSelectionEnd(buffer);

    int selStart = Math.min(a, b);
    int selEnd = Math.max(a, b);

    if (selStart < 0) {
        if (buffer.getSpanStart(FROM_BELOW) >= 0) {
            selStart = selEnd = buffer.length();
        }
    }

    if (selStart > last)
        selStart = selEnd = Integer.MAX_VALUE;
    if (selEnd < first)
        selStart = selEnd = -1;

    switch (what) {
    case CLICK:
        if (selStart == selEnd) {
            return false;
        }

        ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);

        if (link.length != 1)
            return false;

        link[0].onClick(widget);
        break;

    case UP:
        int bestStart, bestEnd;

        bestStart = -1;
        bestEnd = -1;

        for (int i = 0; i < candidates.length; i++) {
            int end = buffer.getSpanEnd(candidates[i]);

            if (end < selEnd || selStart == selEnd) {
                if (end > bestEnd) {
                    bestStart = buffer.getSpanStart(candidates[i]);
                    bestEnd = end;
                }
            }
        }

        if (bestStart >= 0) {
            Selection.setSelection(buffer, bestEnd, bestStart);
            return true;
        }

        break;

    case DOWN:
        bestStart = Integer.MAX_VALUE;
        bestEnd = Integer.MAX_VALUE;

        for (int i = 0; i < candidates.length; i++) {
            int start = buffer.getSpanStart(candidates[i]);

            if (start > selStart || selStart == selEnd) {
                if (start < bestStart) {
                    bestStart = start;
                    bestEnd = buffer.getSpanEnd(candidates[i]);
                }
            }
        }

        if (bestEnd < Integer.MAX_VALUE) {
            Selection.setSelection(buffer, bestStart, bestEnd);
            return true;
        }

        break;
    }

    return false;
}