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

The following examples show how to use android.text.Spanned#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: HighlightingEditor.java    From writeily-pro with MIT License 6 votes vote down vote up
@Override
public CharSequence filter(
        CharSequence source,
        int start,
        int end,
        Spanned dest,
        int dstart,
        int dend) {

    if (modified &&
            end - start == 1 &&
            start < source.length() &&
            dstart < dest.length()) {
        char c = source.charAt(start);

        if (c == '\n')
            return autoIndent(
                    source,
                    dest,
                    dstart,
                    dend);
    }

    return source;
}
 
Example 2
Source File: Utilities.java    From google-authenticator-android with Apache License 2.0 6 votes vote down vote up
private static Spanned removeTrailingNewlines(Spanned text) {
  int trailingNewlineCharacterCount = 0;
  for (int i = text.length() - 1; i >= 0; i--) {
    char c = text.charAt(i);
    if ((c == '\n') || (c == '\r')) {
      trailingNewlineCharacterCount++;
    } else {
      break;
    }
  }
  if (trailingNewlineCharacterCount == 0) {
    return text;
  }

  return new SpannedString(
      text.subSequence(0, text.length() - trailingNewlineCharacterCount));
}
 
Example 3
Source File: Html.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();
    int next;
    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);
        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 4
Source File: SelectionPopupController.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private boolean hasStyleSpan(Spanned spanned) {
    // Only check against those three classes below, which could affect text appearance, since
    // there are other kind of classes won't affect appearance.
    Class<?>[] styleClasses = {
            CharacterStyle.class, ParagraphStyle.class, UpdateAppearance.class};
    for (Class<?> clazz : styleClasses) {
        if (spanned.nextSpanTransition(-1, spanned.length(), clazz) < spanned.length()) {
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: KnifeParser.java    From Knife with Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int next;

    for (int i = 0; i < text.length(); i = next) {
        next = text.nextSpanTransition(i, text.length(), ParagraphStyle.class);

        ParagraphStyle[] styles = text.getSpans(i, next, ParagraphStyle.class);
        if (styles.length == 2) {
            if (styles[0] instanceof BulletSpan && styles[1] instanceof QuoteSpan) {
                // Let a <br> follow the BulletSpan or QuoteSpan end, so next++
                withinBulletThenQuote(out, text, i, next++);
            } else if (styles[0] instanceof QuoteSpan && styles[1] instanceof BulletSpan) {
                withinQuoteThenBullet(out, text, i, next++);
            } else {
                withinContent(out, text, i, next);
            }
        } else if (styles.length == 1) {
            if (styles[0] instanceof BulletSpan) {
                withinBullet(out, text, i, next++);
            } else if (styles[0] instanceof QuoteSpan) {
                withinQuote(out, text, i, next++);
            } else {
                withinContent(out, text, i, next);
            }
        } else {
            withinContent(out, text, i, next);
        }
    }
}
 
Example 6
Source File: Clipboard.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private boolean hasStyleSpan(Spanned spanned) {
    Class<?>[] styleClasses = {
            CharacterStyle.class, ParagraphStyle.class, UpdateAppearance.class};
    for (Class<?> clazz : styleClasses) {
        if (spanned.nextSpanTransition(-1, spanned.length(), clazz) < spanned.length()) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: PosOrZeroIntegerRow.java    From dhis2-android-datacapture with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CharSequence filter(CharSequence str, int start, int end,
        Spanned spn, int spStart, int spEnd) {
    
    if ((str.length() > 0) && (spn.length() > 0) && (spn.charAt(0) == '0')) {
        return Field.EMPTY_FIELD;
    }
    
    if ((spn.length() > 0) && (spStart == 0) && (str.length() > 0) && (str.charAt(0) == '0')) {
        return Field.EMPTY_FIELD;
    }
   
    return str;
}
 
Example 8
Source File: Html.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); 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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 9
Source File: MyHtml.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); 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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 10
Source File: RecipientsEditor.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public List<String> getNumbers() {
    Spanned sp = mList.getText();
    int len = sp.length();
    List<String> list = new ArrayList<String>();

    int start = 0;
    int i = 0;
    while (i < len + 1) {
        char c;
        if ((i == len) || ((c = sp.charAt(i)) == ',') || (c == ';')) {
            if (i > start) {
                list.add(getNumberAt(sp, start, i, mContext));

                // calculate the recipients total length. This is so if the name contains
                // commas or semis, we'll skip over the whole name to the next
                // recipient, rather than parsing this single name into multiple
                // recipients.
                int spanLen = getSpanLength(sp, start, i, mContext);
                if (spanLen > i) {
                    i = spanLen;
                }
            }

            i++;

            while ((i < len) && (sp.charAt(i) == ' ')) {
                i++;
            }

            start = i;
        } else {
            i++;
        }
    }

    return list;
}
 
Example 11
Source File: HtmlCompat.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(Context context, StringBuilder out, Spanned text, int option) {
    int len = text.length();
    int next;
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] styles = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;
        for (ParagraphStyle style : styles) {
            if (style instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style).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(context, out, text, i, next, option);
        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 12
Source File: Html.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); 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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 13
Source File: DigitsInputFilter.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private String deleteCharAtIndex(Spanned dest, int dstart) {
    StringBuilder builder = new StringBuilder(dest);
    if (dest.length() > 0) {
        builder.deleteCharAt(dstart);
    }
    return builder.toString();
}
 
Example 14
Source File: HtmlEx.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private /* static */ void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();

    int next;
    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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 15
Source File: Html.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
        int len = text.length();

        int next;
        for (int i = 0; i < len; i = next) {
            next = text.nextSpanTransition(i, len, IBlockStyle.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);

//            if (needDiv) {
//                out.append("</div>");
//            }
        }
    }
 
Example 16
Source File: Load.java    From ZadakNotification with MIT License 5 votes vote down vote up
public Load message(@NonNull Spanned messageSpanned) {
    if (messageSpanned.length() == 0) {
        throw new IllegalArgumentException("Message Must Not Be Empty!");
    }

    this.messageSpanned = messageSpanned;
    this.builder.setContentText(messageSpanned);
    return this;
}
 
Example 17
Source File: HexEdit.java    From hsv-alpha-color-picker-android with Apache License 2.0 5 votes vote down vote up
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
	// If 8 digits have been pasted, replacing all source, trim alpha digits.
	// Otherwise standard LengthFilter behavior.
	final int srcLength = end - start;
	final int dstSelLength = dend - dstart;
	if (srcLength == PASTED_LEN && dstSelLength == dest.length()) {
		// Discard alpha digits:
		return source.subSequence(PASTED_LEN - MAX_LENGTH, PASTED_LEN);
	}
	else {
		return sixDigitFilter.filter(source, start, end, dest, dstart, dend);
	}
}
 
Example 18
Source File: Html.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); 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);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example 19
Source File: RecipientsEditor.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public List<String> getNumbers() {
    Spanned sp = mList.getText();
    int len = sp.length();
    List<String> list = new ArrayList<String>();

    int start = 0;
    int i = 0;
    while (i < len + 1) {
        char c;
        if ((i == len) || ((c = sp.charAt(i)) == ',') || (c == ';')) {
            if (i > start) {
                list.add(getNumberAt(sp, start, i, mContext));

                // calculate the recipients total length. This is so if the name contains
                // commas or semis, we'll skip over the whole name to the next
                // recipient, rather than parsing this single name into multiple
                // recipients.
                int spanLen = getSpanLength(sp, start, i, mContext);
                if (spanLen > i) {
                    i = spanLen;
                }
            }

            i++;

            while ((i < len) && (sp.charAt(i) == ' ')) {
                i++;
            }

            start = i;
        } else {
            i++;
        }
    }

    return list;
}
 
Example 20
Source File: RichEditText.java    From GSYRickText with MIT License 4 votes vote down vote up
/**
 * 处理at某人
 *
 * @param text      输入文本
 * @param spannable 处理过的文本
 * @param color     颜色
 * @param listUser  用户列表
 * @return Spannable
 */
private Spannable resolveAtInsert(String text, Spannable spannable, String color, List<UserModel> listUser) {

    if (listUser == null || listUser.size() <= 0) {
        return spannable;
    }

    //此处保存名字的键值
    Map<String, String> names = new HashMap<>();
    if (listUser.size() > 0) {
        for (UserModel userModel : listUser) {
            names.put(userModel.getUser_name(), userModel.getUser_name());
        }
    }
    int length = spannable.length();
    Pattern pattern = Pattern.compile("@[^\\s]+\\s?");
    Matcher matcher = pattern.matcher(spannable);
    SpannableStringBuilder spannableStringBuilder =
            new SpannableStringBuilder(spannable);
    for (int i = 0; i < length; i++) {
        if (matcher.find()) {
            String name = text.substring(matcher.start(), matcher.end());
            if (names.containsKey(name.replace("\b", "").replace(" ", ""))) {
                //直接用span会导致后面没文字的时候新输入的一起变色
                Spanned htmlText = Html.fromHtml(String.format("<font color='%s'>" + name + "</font>", color));
                spannableStringBuilder.replace(matcher.start(), matcher.start() + name.length(), htmlText);
                int index = matcher.start() + htmlText.length();
                if (index < text.length()) {
                    if (" ".equals(text.subSequence(index - 1, index))) {
                        spannableStringBuilder.replace(index - 1, index, "\b");
                    }
                } else {
                    if (text.substring(index - 1).equals(" ")) {
                        spannableStringBuilder.replace(index - 1, index, "\b");
                    } else {
                        //如果是最后面的没有空格,补上\b
                        spannableStringBuilder.insert(index, "\b");
                    }
                }
            }
        }
    }
    return spannableStringBuilder;
}