Java Code Examples for android.text.Spanned#toString()

The following examples show how to use android.text.Spanned#toString() . 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: TestSpanMatcher.java    From Markwon with Apache License 2.0 6 votes vote down vote up
public static void documentMatches(
        @NonNull Spanned spanned,
        @NonNull TestSpan.Document document) {

    // match full text

    final String expected = document.wholeText();
    final String actual = spanned.toString();

    if (!expected.equals(actual)) {
        throw new ComparisonFailure(
                "Document text mismatch",
                expected,
                actual);
    }
}
 
Example 2
Source File: RTLayout.java    From memoir with Apache License 2.0 6 votes vote down vote up
public RTLayout(Spanned spanned) {
    if (spanned != null) {
        String s = spanned.toString();
        int len = s.length();

        // find the line breaks and the according lines / paragraphs
        mNrOfLines = 1;
        Matcher m = LINEBREAK_PATTERN.matcher(s.substring(0, len));
        int groupStart = 0;
        while (m.find()) {
            // the line feeds are part of the paragraph              isFirst          isLast
            Paragraph paragraph = new Paragraph(groupStart, m.end(), mNrOfLines == 1, false);
            mParagraphs.add(paragraph);
            groupStart = m.end();
            mNrOfLines++;
        }

        // even an empty line after the last cr/lf is considered a paragraph
        if (mParagraphs.size() < mNrOfLines) {
            mParagraphs.add(new Paragraph(groupStart, len, mNrOfLines == 1, true));
        }
    }
}
 
Example 3
Source File: RTLayout.java    From memoir with Apache License 2.0 6 votes vote down vote up
public RTLayout(Spanned spanned) {
    if (spanned != null) {
        String s = spanned.toString();
        int len = s.length();

        // find the line breaks and the according lines / paragraphs
        mNrOfLines = 1;
        Matcher m = LINEBREAK_PATTERN.matcher(s.substring(0, len));
        int groupStart = 0;
        while (m.find()) {
            // the line feeds are part of the paragraph              isFirst          isLast
            Paragraph paragraph = new Paragraph(groupStart, m.end(), mNrOfLines == 1, false);
            mParagraphs.add(paragraph);
            groupStart = m.end();
            mNrOfLines++;
        }

        // even an empty line after the last cr/lf is considered a paragraph
        if (mParagraphs.size() < mNrOfLines) {
            mParagraphs.add(new Paragraph(groupStart, len, mNrOfLines == 1, true));
        }
    }
}
 
Example 4
Source File: TextUtils.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns displayable styled text from the provided HTML string.
 * <br/>
 * Note: Also handles the case when a String has multiple HTML entities that translate to one
 * character e.g. {@literal &amp;#39;} which is essentially an apostrophe that should normally
 * occur as {@literal &#39;}
 *
 * @param html The source string having HTML content.
 * @return Formatted HTML.
 */
@NonNull
public static Spanned formatHtml(@NonNull String html) {
    final String REGEX = "(&#?[a-zA-Z0-9]+;)";
    final Pattern PATTERN = Pattern.compile(REGEX);

    Spanned formattedHtml = new SpannedString(html);
    String previousHtml = null;

    // Break the loop if there isn't an HTML entity in the text or when all the HTML entities
    // have been decoded. Also break the loop in the special case when a String having the
    // same format as an HTML entity is left but it isn't essentially a decodable HTML entity
    // e.g. &#asdfasd;
    while (PATTERN.matcher(html).find() && !html.equals(previousHtml)) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            formattedHtml = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
        } else {
            formattedHtml = Html.fromHtml(html);
        }
        previousHtml = html;
        html = formattedHtml.toString();
    }

    return formattedHtml;
}
 
Example 5
Source File: IntegerSizeFilter.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
@Override
public CharSequence filter(CharSequence source, int start, int end,
                           Spanned dest, int dstart, int dend) {
    String destString = dest.toString();
    if (source.equals("") || destString.equals("")) {
        return null; //If the source or destination strings are empty, can leave as is
    }
    String part1 = destString.substring(0, dstart);
    String part2 = destString.substring(dend);
    String newString = part1 + source.subSequence(start, end).toString() + part2;

    try {
        Integer x = Integer.parseInt(newString);
        return null; //keep original
    } catch (NumberFormatException e) {
        return ""; //don't allow edit that was just made
    }
}
 
Example 6
Source File: SettingsActivity.java    From ns-usbloader-mobile with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CharSequence filter(CharSequence charSequence, int start, int end, Spanned destination, int dStart, int dEnd) {
    if (end > start) {
        String destTxt = destination.toString();
        String resultingTxt = destTxt.substring(0, dStart) +
                charSequence.subSequence(start, end) +
                destTxt.substring(dEnd);
        if (!resultingTxt.matches ("^[0-9]+"))
            return "";
        if (Integer.valueOf(resultingTxt) > 65535)
            return "";
    }
    return null;
}
 
Example 7
Source File: JumpingBeans.java    From stynico with MIT License 5 votes vote down vote up
private static CharSequence removeJumpingBeansSpansFrom(Spanned text) {
    SpannableStringBuilder sbb = new SpannableStringBuilder(text.toString());
    Object[] spans = text.getSpans(0, text.length(), Object.class);
    for (Object span : spans) {
        if (!(span instanceof JumpingBeansSpan)) {
            sbb.setSpan(span, text.getSpanStart(span), text.getSpanEnd(span), text.getSpanFlags(span));
        }
    }
    return sbb;
}
 
Example 8
Source File: MainActivity.java    From star-dns-changer with MIT License 5 votes vote down vote up
private void initViews() {
    toolbar.setTitle("");
    setSupportActionBar(toolbar);
    logo.bringToFront();
    logo.requestLayout();
    logo.invalidate();

    InputFilter[] filters = new InputFilter[1];
    filters[0] = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start,
                                   int end, Spanned dest, int dstart, int dend) {
            if (end > start) {
                String destTxt = dest.toString();
                String resultingTxt = destTxt.substring(0, dstart) +
                        source.subSequence(start, end) +
                        destTxt.substring(dend);
                if (!resultingTxt.matches("^\\d{1,3}(\\." +
                        "(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
                    return "";
                } else {
                    String[] splits = resultingTxt.split("\\.");
                    for (int i = 0; i < splits.length; i++) {
                        if (Integer.valueOf(splits[i]) > 255) {
                            return "";
                        }
                    }
                }
            }
            return null;
        }
    };
    firstDnsEdit.setFilters(filters);
    secondDnsEdit.setFilters(filters);
    ViewCompat.setTranslationZ(logo, 8);
}
 
Example 9
Source File: JumpingBeans.java    From BookLoadingView with Apache License 2.0 5 votes vote down vote up
private static CharSequence removeJumpingBeansSpansFrom(Spanned text) {
    SpannableStringBuilder sbb = new SpannableStringBuilder(text.toString());
    Object[] spans = text.getSpans(0, text.length(), Object.class);
    for (Object span : spans) {
        if (!(span instanceof JumpingBeansSpan)) {
            sbb.setSpan(span, text.getSpanStart(span),
                    text.getSpanEnd(span), text.getSpanFlags(span));
        }
    }
    return sbb;
}
 
Example 10
Source File: JumpingBeans.java    From AutoLoadListView with Apache License 2.0 5 votes vote down vote up
private static CharSequence removeJumpingBeansSpans(Spanned text) {
    SpannableStringBuilder sbb = new SpannableStringBuilder(text.toString());
    Object[] spans = text.getSpans(0, text.length(), Object.class);
    for (Object span : spans) {
        if (!(span instanceof JumpingBeansSpan)) {
            sbb.setSpan(span, text.getSpanStart(span),
                    text.getSpanEnd(span), text.getSpanFlags(span));
        }
    }
    return sbb;
}
 
Example 11
Source File: JumpingBeans.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private static CharSequence removeJumpingBeansSpans(Spanned text) {
    SpannableStringBuilder sbb = new SpannableStringBuilder(text.toString());
    Object[] spans = text.getSpans(0, text.length(), Object.class);
    for (Object span : spans) {
        if (!(span instanceof JumpingBeansSpan)) {
            sbb.setSpan(span, text.getSpanStart(span),
                    text.getSpanEnd(span), text.getSpanFlags(span));
        }
    }
    return sbb;
}
 
Example 12
Source File: JumpingBeans.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private static CharSequence removeJumpingBeansSpans(Spanned text) {
    SpannableStringBuilder sbb = new SpannableStringBuilder(text.toString());
    Object[] spans = text.getSpans(0, text.length(), Object.class);
    for (Object span : spans) {
        if (!(span instanceof JumpingBeansSpan)) {
            sbb.setSpan(span, text.getSpanStart(span),
                    text.getSpanEnd(span), text.getSpanFlags(span));
        }
    }
    return sbb;
}
 
Example 13
Source File: ComponentDigitCtrlFilter.java    From Android with MIT License 4 votes vote down vote up
/**
 *  source    The new input string
 *  start    New start index to the string that is input, average is 0
 *  end    The new input string at the end of the subscript, general source in length - 1
 *  dest    Before the input text box
 *  dstart    The original contents starting coordinates, general is 0
 *  dend    The original contents coordinates, generally for the dest length - 1
 */

@Override
public CharSequence filter(CharSequence src, int start, int end,
                           Spanned dest, int dstart, int dend) {
    String oldtext =  dest.toString();
    System.out.println(oldtext);
    if ("".equals(src.toString())) {
        return null;
    }
    Matcher m = p.matcher(src);
    if(oldtext.contains(".")){
        if(!m.matches()){
            return null;
        }
    }else{
        if(!m.matches() && !src.equals(".") ){
            return null;
        }
    }
    if(!src.toString().equals("")){
        if((oldtext+src.toString()).equals(".")){
            return "";
        }
        StringBuffer oldStr = new StringBuffer(oldtext);
        oldStr.insert(dstart,src + "");
        double dold = Double.parseDouble(oldStr.toString());
        if(dold > maxValue){
            return dest.subSequence(dstart, dend);
        }else if(dold == maxValue){
            if(src.toString().equals(".")){
                return dest.subSequence(dstart, dend);
            }
        }
    }
    if(oldtext.contains(".")){
        int index = oldtext.indexOf(".");
        int len = oldtext.length()-1 + end - index;

        if(len > pontintLength){
            CharSequence newText = dest.subSequence(dstart, dend);
            return newText;
        }
    }
    return dest.subSequence(dstart, dend) +src.toString();
}
 
Example 14
Source File: EditInputFilterPrice.java    From Android with MIT License 4 votes vote down vote up
@Override
public CharSequence filter(CharSequence src, int start, int end,
                           Spanned dest, int dstart, int dend) {
    String oldtext =  dest.toString();
    System.out.println(oldtext);
    if ("".equals(src.toString())) {
        return null;
    }
    Matcher m = p.matcher(src);
    if(oldtext.contains(".")){
        if(!m.matches()){
            return null;
        }
    }else{
        if(!m.matches() && !src.equals(".") ){
            return null;
        }
    }
    if(!src.toString().equals("") && RegularUtil.matches(oldtext, RegularUtil.VERIFICATION_AMOUT)) {
        if((oldtext+src.toString()).equals(".")){
            return "";
        }
        StringBuffer oldStr = new StringBuffer(oldtext);
        oldStr.insert(dstart,src + "");
        double dold = Double.parseDouble(oldStr.toString());
        if(dold > maxValue){
            return dest.subSequence(dstart, dend);
        }else if(dold == maxValue){
            if(src.toString().equals(".")){
                return dest.subSequence(dstart, dend);
            }
        }
    }
    if(oldtext.contains(".")){
        int index = oldtext.indexOf(".");
        int len = oldtext.length() - 1 - index;
        if(index < dstart){
            len ++;
        }

        if(len > pontintLength){
            CharSequence newText = dest.subSequence(dstart, dend);
            return newText;
        }
    }
    return dest.subSequence(dstart, dend) +src.toString();
}