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

The following examples show how to use android.text.SpannableStringBuilder#getSpanEnd() . 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: TimelineListAdapter.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Link clickable.
 *
 * @param strBuilder
 *   A string builder.
 * @param span
 *   The span with url.
 */
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            try {
                CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
                intentBuilder.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary));
                intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
                CustomTabsIntent customTabsIntent = intentBuilder.build();
                customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                customTabsIntent.launchUrl(context, Uri.parse(span.getURL()));
            }
            catch (Exception ignored) { }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 2
Source File: DefaultDecorator.java    From kaif-android with Apache License 2.0 6 votes vote down vote up
private static <T> void end(SpannableStringBuilder text, Class<T> kind, Object repl) {
  int len = text.length();
  T obj = getLast(text, kind);
  int where = text.getSpanStart(obj);
  text.removeSpan(obj);

  Object[] nestSpans = text.getSpans(where, len, Object.class);
  List<NestSpanInfo> spans = new ArrayList<>();
  for (Object nestSpan : nestSpans) {
    int spanStart = text.getSpanStart(nestSpan);
    int spanEnd = text.getSpanEnd(nestSpan);
    int spanFlags = text.getSpanFlags(nestSpan);
    text.removeSpan(nestSpan);
    spans.add(new NestSpanInfo(nestSpan, spanStart, spanEnd, spanFlags));
  }

  if (where != len) {
    text.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  //reorder spans
  for (NestSpanInfo span : spans) {
    text.setSpan(span.span, span.start, span.end, span.flag);
  }
}
 
Example 3
Source File: GoURLSpan.java    From droidddle with Apache License 2.0 6 votes vote down vote up
/**
 * @param spanText
 * @return true if have url
 */
public static final boolean hackURLSpanHasResult(SpannableStringBuilder spanText) {
    boolean result = false;
    URLSpan[] spans = spanText.getSpans(0, spanText.length(), URLSpan.class);
    // TODO URLSpan need change to ClickableSpan (GoURLSpan) , otherwise URLSpan can not click, not display underline.WHY?
    for (URLSpan span : spans) {
        int start = spanText.getSpanStart(span);
        int end = spanText.getSpanEnd(span);
        String url = span.getURL();
        if (url != null) {
            result = true;
            spanText.removeSpan(span);
            ClickableSpan span1 = new GoURLSpan(span.getURL());
            spanText.setSpan(span1, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    return result;
}
 
Example 4
Source File: InterfaceFragment.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
@Override
public void onPreferenceAfterChange(Preference preference) {
	super.onPreferenceAfterChange(preference);
	if (preference == advancedSearchPreference && advancedSearchPreference.isChecked()) {
		SpannableStringBuilder builder = new SpannableStringBuilder
				(getText(R.string.preference_advanced_search_message));
		Object[] spans = builder.getSpans(0, builder.length(), Object.class);
		for (Object span : spans) {
			int start = builder.getSpanStart(span);
			int end = builder.getSpanEnd(span);
			int flags = builder.getSpanFlags(span);
			builder.removeSpan(span);
			builder.setSpan(new TypefaceSpan("sans-serif-medium"), start, end, flags);
		}
		MessageDialog.create(this, builder, false);
	}
}
 
Example 5
Source File: ImageSpanTarget.java    From android-proguards with Apache License 2.0 6 votes vote down vote up
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
    TextView tv = textView.get();
    if (tv != null) {
        BitmapDrawable bitmapDrawable = new BitmapDrawable(tv.getResources(), bitmap);
        // image span doesn't handle scaling so we manually set bounds
        if (bitmap.getWidth() > tv.getWidth()) {
            float aspectRatio = (float) bitmap.getHeight() / (float) bitmap.getWidth();
            bitmapDrawable.setBounds(0, 0, tv.getWidth(), (int) (aspectRatio * tv.getWidth()));
        } else {
            bitmapDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
        }
        ImageSpan span = new ImageSpan(bitmapDrawable);
        // add the image span and remove our marker
        SpannableStringBuilder ssb = new SpannableStringBuilder(tv.getText());
        int start = ssb.getSpanStart(loadingSpan);
        int end = ssb.getSpanEnd(loadingSpan);
        if (start >= 0 && end >= 0) {
            ssb.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        ssb.removeSpan(loadingSpan);
        // animate the change
        TransitionManager.beginDelayedTransition((ViewGroup) tv.getParent());
        tv.setText(ssb);
    }
}
 
Example 6
Source File: AdapterActManage.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder,
                              final URLSpan urlSpan) {
    int start = clickableHtmlBuilder.getSpanStart(urlSpan);
    int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
    int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
    clickableHtmlBuilder.setSpan(new ClickableSpan() {
        public void onClick(View view) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(urlSpan.getURL()));
                context.startActivity(intent);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }, start, end, flags);
}
 
Example 7
Source File: MessageFormatter.java    From talk-android with MIT License 6 votes vote down vote up
public static Spannable formatHighlightSpan(String html, Resources res) {
    HighlightSpan highlightSpan;
    if (Html.fromHtml(html) instanceof SpannableStringBuilder) {
        SpannableStringBuilder value = (SpannableStringBuilder) Html.fromHtml(html);
        StyleSpan[] spans = value.getSpans(0, html.length(), StyleSpan.class);
        for (StyleSpan span : spans) {
            int start = value.getSpanStart(span);
            int end = value.getSpanEnd(span);
            value.removeSpan(span);
            highlightSpan = new HighlightSpan(res);
            value.setSpan(highlightSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return value;
    } else {
        return new SpannableStringBuilder(html);
    }
}
 
Example 8
Source File: AppUtils.java    From quill with MIT License 6 votes vote down vote up
public static void setHtmlWithLinkClickHandler(TextView tv, String html,
                                        Action1<String> linkClickHandler) {
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (URLSpan span : urls) {
        int start = strBuilder.getSpanStart(span);
        int end = strBuilder.getSpanEnd(span);
        int flags = strBuilder.getSpanFlags(span);
        ClickableSpan clickable = new ClickableSpan() {
            public void onClick(View view) {
                linkClickHandler.call(span.getURL());
            }
        };
        strBuilder.setSpan(clickable, start, end, flags);
        strBuilder.removeSpan(span);
    }
    tv.setText(strBuilder);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
}
 
Example 9
Source File: InteractionUnit.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
String getCopyReadyComment(CharSequence text, int start, int end) {
	if (text instanceof Spanned) {
		SpannableStringBuilder builder = new SpannableStringBuilder(text.subSequence(start, end));
		LinkSuffixSpan[] spans = builder.getSpans(0, builder.length(), LinkSuffixSpan.class);
		if (spans != null && spans.length > 0) {
			for (LinkSuffixSpan span : spans) {
				int spanStart = builder.getSpanStart(span);
				int spanEnd = builder.getSpanEnd(span);
				builder.delete(spanStart, spanEnd);
			}
		}
		return builder.toString();
	} else {
		return text.subSequence(start, end).toString();
	}
}
 
Example 10
Source File: TimelineDetailActivity.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Link clickable.
 *
 * @param strBuilder
 *   A string builder.
 * @param span
 *   The span with url.
 */
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(@NonNull View view) {
            try {
                CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
                intentBuilder.setToolbarColor(ContextCompat.getColor(TimelineDetailActivity.this, R.color.colorPrimary));
                intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(TimelineDetailActivity.this, R.color.colorPrimaryDark));
                CustomTabsIntent customTabsIntent = intentBuilder.build();
                customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                customTabsIntent.launchUrl(TimelineDetailActivity.this, Uri.parse(span.getURL()));
            }
            catch (Exception ignored) { }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 11
Source File: ConverterHtmlToSpanned.java    From memoir with Apache License 2.0 5 votes vote down vote up
void swapIn(SpannableStringBuilder builder) {
    int start = builder.getSpanStart(this);
    int end = builder.getSpanEnd(this);
    builder.removeSpan(this);
    if (start >= 0 && end > start && end <= builder.length()) {
        builder.setSpan(mSpan, start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    }
}
 
Example 12
Source File: ConverterHtmlToSpanned.java    From memoir with Apache License 2.0 5 votes vote down vote up
void swapIn(SpannableStringBuilder builder) {
    int start = builder.getSpanStart(this);
    int end = builder.getSpanEnd(this);
    builder.removeSpan(this);
    if (start >= 0 && end > start && end <= builder.length()) {
        builder.setSpan(mSpan, start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    }
}
 
Example 13
Source File: ChartDataUsageView.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static void setText(
        SpannableStringBuilder builder, Object key, CharSequence text, String bootstrap) {
    int start = builder.getSpanStart(key);
    int end = builder.getSpanEnd(key);
    if (start == -1) {
        start = TextUtils.indexOf(builder, bootstrap);
        end = start + bootstrap.length();
        builder.setSpan(key, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    }
    builder.replace(start, end, text);
}
 
Example 14
Source File: TagHandlerImpl.java    From Markdown with MIT License 5 votes vote down vote up
private boolean checkInCode(SpannableStringBuilder builder, int start, int end) {
    CodeSpan[] css = builder.getSpans(0, builder.length(), CodeSpan.class);
    for (CodeSpan cs : css) {
        int c_start = builder.getSpanStart(cs);
        int c_end = builder.getSpanEnd(cs);
        if (!(c_start >= end || c_end <= start)) {
            return true;
        }
    }
    return false;
}
 
Example 15
Source File: Style.java    From RichEditor with MIT License 5 votes vote down vote up
@Override
public void beforeStyle(SpannableStringBuilder ssb, int start, int end) {
    HeadSpan[] headSpans = ssb.getSpans(start, end, HeadSpan.class);
    for (HeadSpan headSpan : headSpans) {
        int s = ssb.getSpanStart(headSpan);
        int e = ssb.getSpanEnd(headSpan);
        ssb.removeSpan(headSpan);
        ssb.setSpan(headSpan, s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
 
Example 16
Source File: GDPRViewManager.java    From GDPRDialog with Apache License 2.0 5 votes vote down vote up
private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span, Runnable runnable) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            runnable.run();
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 17
Source File: GlobalGUIRoutines.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
private static SpannableStringBuilder addBullets(Spanned htmlSpanned) {
    SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(htmlSpanned);
    BulletSpan[] spans = spannableBuilder.getSpans(0, spannableBuilder.length(), BulletSpan.class);
    if (spans != null) {
        for (BulletSpan span : spans) {
            int start = spannableBuilder.getSpanStart(span);
            int end  = spannableBuilder.getSpanEnd(span);
            spannableBuilder.removeSpan(span);
            spannableBuilder.setSpan(new ImprovedBulletSpan(dip(2), dip(8), 0), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    }
    return spannableBuilder;
}
 
Example 18
Source File: JellyBeanSpanFixTextView.java    From v2ex with Apache License 2.0 5 votes vote down vote up
private FixingResult addSpacesAroundSpansUntilFixed(SpannableStringBuilder builder,
                                                    int widthMeasureSpec, int heightMeasureSpec) {

    Object[] spans = builder.getSpans(0, builder.length(), Object.class);
    List<Object> spansWithSpacesBefore = new ArrayList<Object>(spans.length);
    List<Object> spansWithSpacesAfter = new ArrayList<Object>(spans.length);

    for (Object span : spans) {
        int spanStart = builder.getSpanStart(span);
        if (isNotSpace(builder, spanStart - 1)) {
            builder.insert(spanStart, " ");
            spansWithSpacesBefore.add(span);
        }

        int spanEnd = builder.getSpanEnd(span);
        if (isNotSpace(builder, spanEnd)) {
            builder.insert(spanEnd, " ");
            spansWithSpacesAfter.add(span);
        }

        try {
            setTextAndMeasure(builder, widthMeasureSpec, heightMeasureSpec);
            return FixingResult.fixed(spansWithSpacesBefore, spansWithSpacesAfter);
        } catch (IndexOutOfBoundsException notFixed) {
        }
    }
    if (HtmlTextView.DEBUG) {
        Log.d(HtmlTextView.TAG, "Could not fix the Spanned by adding spaces around spans");
    }
    return FixingResult.notFixed();
}
 
Example 19
Source File: ConverterHtmlToSpanned.java    From Android-RTEditor with Apache License 2.0 5 votes vote down vote up
void swapIn(SpannableStringBuilder builder) {
    int start = builder.getSpanStart(this);
    int end = builder.getSpanEnd(this);
    builder.removeSpan(this);
    if (start >= 0 && end > start && end <= builder.length()) {
        builder.setSpan(mSpan, start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    }
}
 
Example 20
Source File: SpanStep2Filter.java    From RichEditor with Apache License 2.0 4 votes vote down vote up
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
    if (charSequence instanceof SpannableStringBuilder) {
        SpannableStringBuilder span = (SpannableStringBuilder) charSequence;
        Object[] spans = span.getSpans(0, span.length(), CharacterStyle.class);
        Iterator iterator = null;
        if (mData.get(position).getSpanList() != null) {
            iterator = mData.get(position).getSpanList().iterator();
        }
        boolean needAdd = false;
        for (int i = 0; i < spans.length; i++) {
            SpanModel model;
            if (iterator != null && iterator.hasNext()) {
                //复用原本的Model,防止重复New
                model = (SpanModel) iterator.next();
                if (model.mSpans != null) {
                    model.mSpans.clear();
                } else {
                    model.mSpans = new ArrayList<>();
                }
            } else {
                RichLog.log("NEW-------------");
                needAdd = true;
                model = new SpanModel();
            }
            model.mSpans.add(spans[i]);
            model.end = span.getSpanEnd(spans[i]);
            model.start = span.getSpanStart(spans[i]);
            for (++i; i < spans.length; i++) {
                if (span.getSpanEnd(spans[i]) == model.end && span.getSpanStart(spans[i]) == model.start) {
                    model.mSpans.add(spans[i]);
                } else {
                    i--;
                    break;
                }
            }
            if (needAdd) {
                mData.get(position).getSpanList().add(model);
            }
        }
        while (!needAdd && iterator != null && iterator.hasNext()) {
            iterator.next();
            iterator.remove();
        }
        for (SpanModel item : mData.get(position).getSpanList()) {
            Log.i(BASE_LOG, item.mSpans + "start:" + item.start + "end:" + item.end);
        }
    }
    mData.get(position).setSource(charSequence.toString());
}