Java Code Examples for android.text.SpannableString#getSpanStart()
The following examples show how to use
android.text.SpannableString#getSpanStart() .
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: ConversationItem.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
private SpannableString linkifyMessageBody(SpannableString messageBody, boolean shouldLinkifyAllLinks) { int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS; boolean hasLinks = Linkify.addLinks(messageBody, shouldLinkifyAllLinks ? linkPattern : 0); if (hasLinks) { Stream.of(messageBody.getSpans(0, messageBody.length(), URLSpan.class)) .filterNot(url -> LinkPreviewUtil.isLegalUrl(url.getURL())) .forEach(messageBody::removeSpan); URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class); for (URLSpan urlSpan : urlSpans) { int start = messageBody.getSpanStart(urlSpan); int end = messageBody.getSpanEnd(urlSpan); messageBody.setSpan(new LongClickCopySpan(urlSpan.getURL()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } return messageBody; }
Example 2
Source File: LinkTransformationMethod.java From natrium-android-wallet with BSD 2-Clause "Simplified" License | 6 votes |
@Override public CharSequence getTransformation(CharSequence source, View view) { if (view instanceof TextView) { TextView textView = (TextView) view; if (textView.getText() == null || !(textView.getText() instanceof Spannable)) { return source; } SpannableString text = (SpannableString) textView.getText(); URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class); for (int i = spans.length - 1; i >= 0; i--) { URLSpan oldSpan = spans[i]; int start = text.getSpanStart(oldSpan); int end = text.getSpanEnd(oldSpan); String url = oldSpan.getURL(); text.removeSpan(oldSpan); text.setSpan(new CustomUrlSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return text; } return source; }
Example 3
Source File: LinkTransformationMethod.java From nano-wallet-android with BSD 2-Clause "Simplified" License | 6 votes |
@Override public CharSequence getTransformation(CharSequence source, View view) { if (view instanceof TextView) { TextView textView = (TextView) view; if (textView.getText() == null || !(textView.getText() instanceof Spannable)) { return source; } SpannableString text = (SpannableString) textView.getText(); URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class); for (int i = spans.length - 1; i >= 0; i--) { URLSpan oldSpan = spans[i]; int start = text.getSpanStart(oldSpan); int end = text.getSpanEnd(oldSpan); String url = oldSpan.getURL(); text.removeSpan(oldSpan); text.setSpan(new CustomUrlSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return text; } return source; }
Example 4
Source File: LinkifyHelper.java From Zom-Android-XMPP with GNU General Public License v3.0 | 6 votes |
public static <A extends CharacterStyle, B extends CharacterStyle> Spannable replaceAll( CharSequence original, Class<A> sourceType, SpanConverter<A, B> converter) { SpannableString result = new SpannableString(original); A[] spans = result.getSpans(0, result.length(), sourceType); for (A span : spans) { int start = result.getSpanStart(span); int end = result.getSpanEnd(span); int flags = result.getSpanFlags(span); result.removeSpan(span); result.setSpan(converter.convert(span), start, end, flags); } return (result); }
Example 5
Source File: TextEventInterpreter.java From talkback with Apache License 2.0 | 6 votes |
@Nullable public static CharSequence getSubsequenceWithSpans( @Nullable CharSequence text, int from, int to) { if (text == null) { return null; } if (from < 0 || text.length() < to || to < from) { return null; } SpannableString textWithSpans = SpannableString.valueOf(text); CharSequence subsequence = text.subSequence(from, to); SpannableString subsequenceWithSpans = SpannableString.valueOf(subsequence); TtsSpan[] spans = subsequenceWithSpans.getSpans(0, subsequence.length(), TtsSpan.class); for (TtsSpan span : spans) { if (textWithSpans.getSpanStart(span) < from || to < textWithSpans.getSpanEnd(span)) { subsequenceWithSpans.removeSpan(span); } } return subsequence; }
Example 6
Source File: PostItem.java From Dashchan with Apache License 2.0 | 6 votes |
public CharSequence getComment(String repliesToPost) { SpannableString comment = new SpannableString(getComment()); LinkSpan[] spans = comment.getSpans(0, comment.length(), LinkSpan.class); if (spans != null) { String commentString = comment.toString(); repliesToPost = ">>" + repliesToPost; for (LinkSpan linkSpan : spans) { int start = comment.getSpanStart(linkSpan); if (commentString.indexOf(repliesToPost, start) == start) { int end = comment.getSpanEnd(linkSpan); comment.setSpan(new StyleSpan(Typeface.BOLD), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return comment; }
Example 7
Source File: Utils.java From openshop.io-android with MIT License | 5 votes |
/** * Method replace ordinary {@link URLSpan} with {@link DefensiveURLSpan}. * * @param spannedText text, where link spans should be replaced. * @param activity activity for displaying problems. * @return text, where link spans are replaced. */ public static SpannableString safeURLSpanLinks(Spanned spannedText, Activity activity) { final SpannableString current = new SpannableString(spannedText); final URLSpan[] spans = current.getSpans(0, current.length(), URLSpan.class); int start, end; for (URLSpan span : spans) { start = current.getSpanStart(span); end = current.getSpanEnd(span); current.removeSpan(span); current.setSpan(new DefensiveURLSpan(span.getURL(), activity), start, end, 0); } return current; }
Example 8
Source File: ConversationItem.java From deltachat-android with GNU General Public License v3.0 | 5 votes |
private SpannableString linkifyMessageBody(SpannableString messageBody, boolean shouldLinkifyAllLinks) { boolean hasLinks = Linkify.addLinks(messageBody, shouldLinkifyAllLinks ? Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS|Linkify.PHONE_NUMBERS : 0); if (hasLinks) { URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class); for (URLSpan urlSpan : urlSpans) { int start = messageBody.getSpanStart(urlSpan); int end = messageBody.getSpanEnd(urlSpan); messageBody.setSpan(new LongClickCopySpan(urlSpan.getURL()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } return messageBody; }
Example 9
Source File: AccessibilityNodeInfoUtils.java From talkback with Apache License 2.0 | 5 votes |
/** * Gets a list of the clickable elements within a node. * * @param node The node to get the elements from * @param clickableType What type of clickable thing to look for within the node * @param clickableElementFn A function taking the visual string representation and the clickable * portion of the clickable element that produces the desired format that will be displayable * to the user * @param <E> The displayable format representation of the clickable element * @return A list of clickable elements. Empty if there are none. */ private static <E> List<E> getNodeClickableElements( AccessibilityNodeInfoCompat node, Class<? extends ClickableSpan> clickableType, Function<Pair<String, ClickableSpan>, E> clickableElementFn) { List<SpannableString> spannableStrings = new ArrayList<>(); SpannableTraversalUtils.collectSpannableStringsWithTargetSpanInNodeDescriptionTree( node, // Root node of description tree clickableType, // Target span class spannableStrings // List to collect spannable strings ); List<E> clickables = new ArrayList<>(1); for (SpannableString spannable : spannableStrings) { for (ClickableSpan span : spannable.getSpans(0, spannable.length(), clickableType)) { if (span instanceof URLSpan && Strings.isNullOrEmpty(((URLSpan) span).getURL())) { continue; } int start = spannable.getSpanStart(span); int end = spannable.getSpanEnd(span); if (end > start) { char[] chars = new char[end - start]; spannable.getChars(start, end, chars, 0); clickables.add(clickableElementFn.apply(Pair.create(new String(chars), span))); } } } return clickables; }
Example 10
Source File: TimeLineUtility.java From iBeebo with GNU General Public License v3.0 | 5 votes |
public static SpannableString convertNormalStringToSpannableString(String txt) { // hack to fix android imagespan bug,see // http://stackoverflow.com/questions/3253148/imagespan-is-cut-off-incorrectly-aligned // if string only contains emotion tags,add a empty char to the end String hackTxt; if (txt.startsWith("[") && txt.endsWith("]")) { hackTxt = txt + " "; } else { hackTxt = txt; } // SpannableString value = SpannableString.valueOf(hackTxt); String formatted = formatLink(hackTxt); Spanned spanned = Html.fromHtml(formatted); SpannableString value = new SpannableString(spanned); Linkify.addLinks(value, WeiboPatterns.MENTION_URL, WeiboPatterns.MENTION_SCHEME); Linkify.addLinks(value, WeiboPatterns.WEB_URL, WeiboPatterns.WEB_SCHEME); Linkify.addLinks(value, WeiboPatterns.TOPIC_URL, WeiboPatterns.TOPIC_SCHEME); URLSpan[] urlSpans = value.getSpans(0, value.length(), URLSpan.class); MyURLSpan weiboSpan; for (URLSpan urlSpan : urlSpans) { weiboSpan = new MyURLSpan(urlSpan.getURL()); int start = value.getSpanStart(urlSpan); int end = value.getSpanEnd(urlSpan); value.removeSpan(urlSpan); value.setSpan(weiboSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } TimeLineUtility.addEmotions(value); return value; }
Example 11
Source File: TimeLineUtility.java From iBeebo with GNU General Public License v3.0 | 5 votes |
public static SpannableString convertNormalStringToSpannableString(String txt) { // hack to fix android imagespan bug,see // http://stackoverflow.com/questions/3253148/imagespan-is-cut-off-incorrectly-aligned // if string only contains emotion tags,add a empty char to the end String hackTxt; if (txt.startsWith("[") && txt.endsWith("]")) { hackTxt = txt + " "; } else { hackTxt = txt; } // SpannableString value = SpannableString.valueOf(hackTxt); String formatted = formatLink(hackTxt); Spanned spanned = Html.fromHtml(formatted); SpannableString value = new SpannableString(spanned); Linkify.addLinks(value, WeiboPatterns.MENTION_URL, WeiboPatterns.MENTION_SCHEME); Linkify.addLinks(value, WeiboPatterns.WEB_URL, WeiboPatterns.WEB_SCHEME); Linkify.addLinks(value, WeiboPatterns.TOPIC_URL, WeiboPatterns.TOPIC_SCHEME); URLSpan[] urlSpans = value.getSpans(0, value.length(), URLSpan.class); MyURLSpan weiboSpan; for (URLSpan urlSpan : urlSpans) { weiboSpan = new MyURLSpan(urlSpan.getURL()); int start = value.getSpanStart(urlSpan); int end = value.getSpanEnd(urlSpan); value.removeSpan(urlSpan); value.setSpan(weiboSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } TimeLineUtility.addEmotions(value); return value; }
Example 12
Source File: TimeLineUtility.java From SprintNBA with Apache License 2.0 | 4 votes |
public static SpannableString convertNormalStringToSpannableString(String txt, TimeLineStatus status) { String hackTxt; if (txt.startsWith("[") && txt.endsWith("]")) { hackTxt = txt + " "; } else { hackTxt = txt; } SpannableString value = SpannableString.valueOf(hackTxt); switch (status) { case LINK: { Linkify.addLinks(value, LinkPatterns.WEB_URL, LinkPatterns.WEB_SCHEME); } break; case FEED: { Linkify.addLinks(value, LinkPatterns.WEB_URL, LinkPatterns.WEB_SCHEME); Linkify.addLinks(value, LinkPatterns.TOPIC_URL, LinkPatterns.TOPIC_SCHEME); Linkify.addLinks(value, LinkPatterns.MENTION_URL, LinkPatterns.MENTION_SCHEME); } break; } android.text.style.URLSpan[] urlSpans = value.getSpans(0, value.length(), android.text.style.URLSpan.class); URLSpan weiboSpan; for (android.text.style.URLSpan urlSpan : urlSpans) { if (urlSpan.getURL().startsWith(LinkPatterns.TOPIC_SCHEME)) { String topic = urlSpan.getURL().substring(LinkPatterns.TOPIC_SCHEME.length(), urlSpan.getURL().length()); //不识别空格话题和大于30字话题 String group = topic.substring(1, topic.length() - 1).trim(); if (1 > group.length() || group.length() > 30) { value.removeSpan(urlSpan); continue; } } weiboSpan = new URLSpan(urlSpan.getURL(), mColor); int start = value.getSpanStart(urlSpan); int end = value.getSpanEnd(urlSpan); value.removeSpan(urlSpan); value.setSpan(weiboSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return value; }
Example 13
Source File: ReviewFragment.java From AnkiDroid-Wear with GNU General Public License v2.0 | 4 votes |
private Spanned makeSoundIconsClickable(Spanned text, boolean isAnswer) { JSONArray sounds = findSounds(isAnswer); SpannableString qss = new SpannableString(text); int soundIndex = 0; ImageSpan[] image_spans = qss.getSpans(0, qss.length(), ImageSpan.class); for (ImageSpan span : image_spans) { if (span.getSource().equals(SOUND_PLACEHOLDER_STRING)) { final String image_src = span.getSource(); final int start = qss.getSpanStart(span); final int end = qss.getSpanEnd(span); final String soundName; String soundName1 = ""; if (soundIndex < sounds.length()) { try { soundName1 = sounds.getString(soundIndex); } catch (JSONException e) { e.printStackTrace(); } } soundName = soundName1; soundIndex++; ClickableString click_span = new ClickableString(onSoundIconClickListener, soundName); // ClickableSpan[] click_spans = qss.getSpans(start, end, // ClickableSpan.class); // // if (click_spans.length != 0) { // // remove all click spans // for (ClickableSpan c_span : click_spans) { // qss.removeSpan(c_span); // } // } qss.setSpan(click_span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } // int soundIndex = 0; // for (int i = 0; i < qss.length()-1; i++){ // if(((int)qss.charAt(i)) == 55357 && ((int)qss.charAt(i+1)) == 56586){ // String soundName = null; // if(soundIndex < sounds.length()){ // try { // soundName = sounds.getString(soundIndex); // } catch (JSONException e) { // e.printStackTrace(); // } // } // // qss.setSpan(new ClickableString(onSoundIconClickListener, soundName), i, i+2, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); // soundIndex++; // } // } } return qss; }