Java Code Examples for android.text.SpannableStringBuilder#subSequence()

The following examples show how to use android.text.SpannableStringBuilder#subSequence() . 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: TagHandlerImpl.java    From Markdown with MIT License 6 votes vote down vote up
@Override
public boolean em(Line line) {
    line = line.get();
    SpannableStringBuilder builder = (SpannableStringBuilder) line.getStyle();
    Matcher matcher = obtain(Tag.EM, builder);
    while (matcher.find()) {
        int start = matcher.start(1);
        int end = matcher.end(1);
        if (checkInCode(builder, start, end)) {
            continue;
        }
        SpannableStringBuilder sb = (SpannableStringBuilder) builder.subSequence(matcher.start(3), matcher.end(3));
        builder.delete(matcher.start(1), matcher.end(1));
        builder.insert(matcher.start(1), styleBuilder.em(sb));
        em(line);
        return true;
    }
    return false;
}
 
Example 2
Source File: TagHandlerImpl.java    From Markdown with MIT License 6 votes vote down vote up
@Override
public boolean italic(Line line) {
    line = line.get();
    SpannableStringBuilder builder = (SpannableStringBuilder) line.getStyle();
    Matcher matcher = obtain(Tag.ITALIC, builder);
    while (matcher.find()) {
        int start = matcher.start(1);
        int end = matcher.end(1);
        if (checkInCode(builder, start, end)) {
            continue;
        }
        SpannableStringBuilder sb = (SpannableStringBuilder) builder.subSequence(matcher.start(3), matcher.end(3));
        builder.delete(matcher.start(1), matcher.end(1));
        builder.insert(matcher.start(1), styleBuilder.italic(sb));
        italic(line);
        return true;
    }
    return false;
}
 
Example 3
Source File: TagHandlerImpl.java    From Markdown with MIT License 6 votes vote down vote up
@Override
public boolean emItalic(Line line) {
    line = line.get();
    SpannableStringBuilder builder = (SpannableStringBuilder) line.getStyle();
    Matcher matcher = obtain(Tag.EM_ITALIC, builder);
    while (matcher.find()) {
        int start = matcher.start(1);
        int end = matcher.end(1);
        if (checkInCode(builder, start, end)) {
            continue;
        }
        SpannableStringBuilder sb = (SpannableStringBuilder) builder.subSequence(matcher.start(3), matcher.end(3));
        builder.delete(matcher.start(1), matcher.end(1));
        builder.insert(matcher.start(1), styleBuilder.emItalic(sb));
        emItalic(line);
        return true;
    }
    return false;
}
 
Example 4
Source File: TagHandlerImpl.java    From Markdown with MIT License 6 votes vote down vote up
@Override
public boolean delete(Line line) {
    line = line.get();
    SpannableStringBuilder builder = (SpannableStringBuilder) line.getStyle();
    Matcher matcher = obtain(Tag.DELETE, builder);
    while (matcher.find()) {
        int start = matcher.start(1);
        int end = matcher.end(1);
        if (checkInCode(builder, start, end)) {
            continue;
        }
        SpannableStringBuilder sb = (SpannableStringBuilder) builder.subSequence(matcher.start(3), matcher.end(3));
        builder.delete(matcher.start(1), matcher.end(1));
        builder.insert(matcher.start(1), styleBuilder.delete(sb));
        delete(line);
        return true;
    }
    return false;
}
 
Example 5
Source File: StyledStringActivity.java    From advanced-textview with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_styled_string);

  SpannableStringBuilder builder = new SpannableStringBuilder()
      .append(formatString(this, R.string.big_red, R.style.BigRedTextAppearance))
      .append("\n")
      .append(formatString(this, R.string.medium_green, R.style.MediumGreenTextAppearance))
      .append("\n")
      .append(formatString(this, R.string.small_blue, R.style.SmallBlueTextAppearance));
  CharSequence styledString = builder.subSequence(0, builder.length());

  TextView textView = (TextView) findViewById(R.id.text);
  textView.setText(styledString);
}
 
Example 6
Source File: GankListAdapter.java    From gank.io-unofficial-android-client with Apache License 2.0 5 votes vote down vote up
@Override
public void onBindViewHolder(ClickableViewHolder holder, int position) {

  if (holder instanceof ItemViewHolder) {
    ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
    Gank.GankInfo gankInfo = mGankList.get(position);
    if (position == 0) {
      showCategory(itemViewHolder);
    } else {
      boolean theCategoryOfLastEqualsToThis = mGankList.get(
          position - 1).type.equals(mGankList.get(position).type);
      if (!theCategoryOfLastEqualsToThis) {
        showCategory(itemViewHolder);
      } else {
        hideCategory(itemViewHolder);
      }
    }
    itemViewHolder.category.setText(gankInfo.type);
    SpannableStringBuilder builder = new SpannableStringBuilder(gankInfo.desc).append(
        StringStyleUtils.format(itemViewHolder.gank.getContext(), " (via. " +
            gankInfo.who +
            ")", R.style.ViaTextAppearance));
    CharSequence gankText = builder.subSequence(0, builder.length());

    itemViewHolder.gank.setText(gankText);
    showItemAnim(itemViewHolder.gank, position);
  }

  super.onBindViewHolder(holder, position);
}
 
Example 7
Source File: TagHandlerImpl.java    From Markdown with MIT License 5 votes vote down vote up
@Override
public boolean email(Line line) {
    line = line.get();
    SpannableStringBuilder builder = (SpannableStringBuilder) line.getStyle();
    Matcher matcher = obtain(Tag.EMAIL, builder);
    if (matcher.find()) {
        SpannableStringBuilder sb = (SpannableStringBuilder) builder.subSequence(matcher.start(2), matcher.end(2));
        builder.delete(matcher.start(1), matcher.end(1));
        builder.insert(matcher.start(1), styleBuilder.email(sb));
        email(line);
        return true;
    }
    return false;
}
 
Example 8
Source File: SpannableStringUtilsTests.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
public void testConcatWithSuggestionSpansOnly() {
    SpannableStringBuilder s = new SpannableStringBuilder("test string\ntest string\n"
            + "test string\ntest string\ntest string\ntest string\ntest string\ntest string\n"
            + "test string\ntest string\n");
    final int N = 10;
    for (int i = 0; i < N; ++i) {
        // Put a PARAGRAPH-flagged span that should not be found in the result.
        s.setSpan(new SuggestionSpan(getContext(),
                new String[] {"" + i}, Spanned.SPAN_PARAGRAPH),
                i * 12, i * 12 + 12, Spanned.SPAN_PARAGRAPH);
        // Put a normal suggestion span that should be found in the result.
        s.setSpan(new SuggestionSpan(getContext(), new String[] {"" + i}, 0), i, i * 2, 0);
        // Put a URL span than should not be found in the result.
        s.setSpan(new URLSpan("http://a"), i, i * 2, 0);
    }

    final CharSequence a = s.subSequence(0, 15);
    final CharSequence b = s.subSequence(15, s.length());
    final Spanned result =
            (Spanned)SpannableStringUtils.concatWithNonParagraphSuggestionSpansOnly(a, b);

    Object[] spans = result.getSpans(0, result.length(), SuggestionSpan.class);
    for (int i = 0; i < spans.length; i++) {
        final int flags = result.getSpanFlags(spans[i]);
        assertEquals("Should not find a span with PARAGRAPH flag",
                flags & Spanned.SPAN_PARAGRAPH, 0);
        assertTrue("Should be a SuggestionSpan", spans[i] instanceof SuggestionSpan);
    }
}
 
Example 9
Source File: StringStyleUtils.java    From GankDaily with GNU General Public License v3.0 4 votes vote down vote up
public static CharSequence getGankInfoSequence(Context context,Gank mGank) {
    SpannableStringBuilder builder = new SpannableStringBuilder(mGank.desc).append(
            StringStyleUtils.format(context, " (via. " + mGank.who + ")", R.style.ViaTextAppearance));
    return builder.subSequence(0, builder.length());
}