Example usage for android.text Spanned length

List of usage examples for android.text Spanned length

Introduction

In this page you can find the example usage for android.text Spanned length.

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static Object getLast(Spanned text, Class<?> kind) {
    /*/*from   w w w.  j ava  2s.  c  o m*/
     * This knows that the last returned object from getSpans()
     * will be the most recently added.
     */
    Object[] objs = text.getSpans(0, text.length(), kind);

    if (objs.length == 0) {
        return null;
    } else {
        return objs[objs.length - 1];
    }
}

From source file:org.miaowo.miaowo.util.Html.java

private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option, Context context) {
    int len = text.length();

    int next;//from  w w w  .j  a v  a 2  s  . com
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align = ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next, option, context);

        if (needDiv) {
            out.append("</div>");
        }
    }
}

From source file:org.miaowo.miaowo.util.Html.java

private static <T> T getLast(Spanned text, Class<T> kind) {
    /*/*from   www .  ja  v a 2  s  . co  m*/
     * This knows that the last returned object from getSpans()
     * will be the most recently added.
     */
    T[] objs = text.getSpans(0, text.length(), kind);

    if (objs.length == 0) {
        return null;
    } else {
        return objs[objs.length - 1];
    }
}

From source file:org.miaowo.miaowo.util.Html.java

private static void withinHtml(StringBuilder out, Spanned text, int option, Context context) {
    if ((option & TO_HTML_PARAGRAPH_FLAG) == TO_HTML_PARAGRAPH_LINES_CONSECUTIVE) {
        encodeTextAlignmentByDiv(out, text, option, context);
        return;//from  w w w . j av a 2s  .  com
    }

    withinDiv(out, text, 0, text.length(), option, context);
}

From source file:com.taobao.weex.dom.WXTextDomObject.java

/**
 * Adjust span range after truncate due to the wrong span range during span copy and slicing.
 * @param beforeTruncate The span before truncate
 * @param afterTruncate The span after truncate
 *///from   w w  w .  j  av a 2  s.com
private void adjustSpansRange(@NonNull Spanned beforeTruncate, @NonNull Spannable afterTruncate) {
    Object[] spans = beforeTruncate.getSpans(0, beforeTruncate.length(), Object.class);
    for (Object span : spans) {
        int start = beforeTruncate.getSpanStart(span);
        int end = beforeTruncate.getSpanEnd(span);
        if (start == 0 && end == beforeTruncate.length()) {
            afterTruncate.removeSpan(span);
            afterTruncate.setSpan(span, 0, afterTruncate.length(), beforeTruncate.getSpanFlags(span));
        }
    }
}

From source file:tk.wasdennnoch.androidn_ify.utils.NotificationColorUtil.java

/**
 * Inverts all the grayscale colors set by {@link TextAppearanceSpan}s on
 * the text./*from   ww  w  .j  av  a 2  s  .  c  om*/
 *
 * @param charSequence The text to process.
 * @return The color inverted text.
 */
public CharSequence invertCharSequenceColors(CharSequence charSequence) {
    if (charSequence instanceof Spanned) {
        Spanned ss = (Spanned) charSequence;
        Object[] spans = ss.getSpans(0, ss.length(), Object.class);
        SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
        for (Object span : spans) {
            Object resultSpan = span;
            if (span instanceof TextAppearanceSpan) {
                resultSpan = processTextAppearanceSpan((TextAppearanceSpan) span);
            }
            builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span), ss.getSpanFlags(span));
        }
        return builder;
    }
    return charSequence;
}

From source file:org.totschnig.myexpenses.util.Utils.java

public static void configDecimalSeparator(final EditText editText, final char decimalSeparator,
        final int fractionDigits) {
    // mAmountText.setInputType(
    // InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
    // due to bug in Android platform
    // http://code.google.com/p/android/issues/detail?id=2626
    // the soft keyboard if it occupies full screen in horizontal orientation
    // does not display the , as comma separator
    // TODO we should take into account the arab separator as well
    final char otherSeparator = decimalSeparator == '.' ? ',' : '.';
    editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    editText.setFilters(new InputFilter[] { new InputFilter() {
        @Override/*  w w w  . jav  a  2 s  .c om*/
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                int dend) {
            int separatorPositionInDest = dest.toString().indexOf(decimalSeparator);
            char[] v = new char[end - start];
            TextUtils.getChars(source, start, end, v, 0);
            String input = new String(v).replace(otherSeparator, decimalSeparator);
            if (fractionDigits == 0 || separatorPositionInDest != -1 || dest.length() - dend > fractionDigits) {
                input = input.replace(String.valueOf(decimalSeparator), "");
            } else {
                int separatorPositionInSource = input.lastIndexOf(decimalSeparator);
                if (separatorPositionInSource != -1) {
                    //we make sure there is only one separator in the input and after the separator we do not use
                    //more minor digits as allowed
                    int existingMinorUnits = dest.length() - dend;
                    int additionalAllowedMinorUnits = fractionDigits - existingMinorUnits;
                    int additionalPossibleMinorUnits = input.length() - separatorPositionInSource - 1;
                    int extractMinorUnits = additionalPossibleMinorUnits >= additionalAllowedMinorUnits
                            ? additionalAllowedMinorUnits
                            : additionalPossibleMinorUnits;
                    input = input.substring(0, separatorPositionInSource)
                            .replace(String.valueOf(decimalSeparator), "") + decimalSeparator
                            + (extractMinorUnits > 0 ? input.substring(separatorPositionInSource + 1,
                                    separatorPositionInSource + 1 + extractMinorUnits) : "");
                }
            }
            if (fractionDigits == 0) {
                return input;
            }
            if (separatorPositionInDest != -1 && dend > separatorPositionInDest
                    && dstart > separatorPositionInDest) {
                int existingMinorUnits = dest.length() - (separatorPositionInDest + 1);
                int remainingMinorUnits = fractionDigits - existingMinorUnits;
                if (remainingMinorUnits < 1) {
                    return "";
                }
                return input.length() > remainingMinorUnits ? input.substring(0, remainingMinorUnits) : input;
            } else {
                return input;
            }
        }
    }, new InputFilter.LengthFilter(16) });
}

From source file:com.googlecode.eyesfree.brailleback.DisplayManager.java

private void markFocus(Spanned spanned) {
    DisplaySpans.FocusSpan[] spans = spanned.getSpans(0, spanned.length(), DisplaySpans.FocusSpan.class);
    for (DisplaySpans.FocusSpan span : spans) {
        int start = textToDisplayPosition(mTranslationResult, mCursorPosition, spanned.getSpanStart(span));
        if (start < mOverlaidBrailleContent.length) {
            copyOverlaidContent();/* w  w  w  .j  a  v  a2  s.co  m*/
            mOverlaidBrailleContent[start] |= FOCUS_DOTS;
            if (mDisplayPosition < 0) {
                mDisplayPosition = fixDisplayPosition(start);
            }
        }
    }
}

From source file:com.googlecode.eyesfree.brailleback.DisplayManager.java

private boolean markSelection(Spanned spanned) {
    DisplaySpans.SelectionSpan[] spans = spanned.getSpans(0, spanned.length(),
            DisplaySpans.SelectionSpan.class);
    for (DisplaySpans.SelectionSpan span : spans) {
        int start = textToDisplayPosition(mTranslationResult, mCursorPosition, spanned.getSpanStart(span));
        int end = textToDisplayPosition(mTranslationResult, mCursorPosition, spanned.getSpanEnd(span));
        if (start == end) {
            end = start + 1;/*from w  ww  . ja  va2  s. c o m*/
        }
        if (end > mBrailleContent.length) {
            extendContentForCursor();
        }
        copyOverlaidContent();
        for (int i = start; i < end && i < mOverlaidBrailleContent.length; ++i) {
            mOverlaidBrailleContent[i] |= SELECTION_DOTS;
        }
        if (mDisplayPosition < 0) {
            mDisplayPosition = fixDisplayPosition(start);
        }
    }
    return spans.length > 0;
}

From source file:com.googlecode.eyesfree.brailleback.DisplayManager.java

private boolean allowContractedBraille(Content content) {
    if (content.getContractionMode() == Content.CONTRACT_ALWAYS_ALLOW) {
        return true;
    }//w  w w .jav a 2  s  .co  m
    Spanned spanned = content.getSpanned();
    if (spanned == null) {
        return true;
    }
    DisplaySpans.SelectionSpan[] selectionSpans = spanned.getSpans(0, spanned.length(),
            DisplaySpans.SelectionSpan.class);
    return selectionSpans.length == 0;
}