android.text.style.URLSpan Java Examples

The following examples show how to use android.text.style.URLSpan. 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: CommentAdapter.java    From Hews with MIT License 6 votes vote down vote up
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) {
            Uri uri = Uri.parse(span.getURL());
            CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
            Utils.setupIntentBuilder(intentBuilder, mContext,
                ((CommentsActivity) mContext).getSharedPreferences());
            ChromeCustomTabsHelper.openCustomTab((Activity) mContext, intentBuilder.build(),
                uri, null);
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example #2
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 #3
Source File: SpanFormatter.java    From tindroid with Apache License 2.0 6 votes vote down vote up
private TreeNode handleButton(final Map<String,Object> data, final Object content) {
    // This is needed for button shadows.
    mContainer.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

    DisplayMetrics metrics = mContainer.getContext().getResources().getDisplayMetrics();
    // Size of a DIP pixel.
    float dipSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.0f, metrics);

    // Create BorderSpan.
    final TreeNode span = new TreeNode(
            (CharacterStyle) new BorderedSpan(mContainer.getContext(), mFontSize, dipSize),
            (CharSequence) null);

    // Wrap URLSpan into BorderSpan.
    span.addNode(new URLSpan("") {
        @Override
        public void onClick(View widget) {
            if (mClicker != null) {
                mClicker.onClick("BN", data);
            }
        }
    }, content);

    return span;
}
 
Example #4
Source File: ConverterSpannedToHtml.java    From memoir with Apache License 2.0 6 votes vote down vote up
private void handleEndTag(CharacterStyle style) {
    if (style instanceof URLSpan) {
        mOut.append("</a>");
    } else if (style instanceof TypefaceSpan) {
        mOut.append("</font>");
    } else if (style instanceof ForegroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof BackgroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof AbsoluteSizeSpan) {
        mOut.append("</font>");
    } else if (style instanceof StrikethroughSpan) {
        mOut.append("</strike>");
    } else if (style instanceof SubscriptSpan) {
        mOut.append("</sub>");
    } else if (style instanceof SuperscriptSpan) {
        mOut.append("</sup>");
    } else if (style instanceof UnderlineSpan) {
        mOut.append("</u>");
    } else if (style instanceof BoldSpan) {
        mOut.append("</b>");
    } else if (style instanceof ItalicSpan) {
        mOut.append("</i>");
    }
}
 
Example #5
Source File: ConversationItem.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 #6
Source File: LinkableLabel.java    From Tehreer-Android with Apache License 2.0 6 votes vote down vote up
private URLSpan urlSpanAtIndex(int charIndex) {
    Spanned spanned = getSpanned();
    ComposedFrame composedFrame = getComposedFrame();

    if (spanned == null || composedFrame == null) {
        return null;
    }
    if (charIndex < composedFrame.getCharStart() || charIndex >= composedFrame.getCharEnd()) {
        return null;
    }

    URLSpan[] spans = spanned.getSpans(charIndex, charIndex, URLSpan.class);
    if (spans != null && spans.length > 0) {
        return spans[0];
    }

    return null;
}
 
Example #7
Source File: HtmlUtils.java    From materialup with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the given input using {@link TouchableUrlSpan}s
 * rather than vanilla {@link URLSpan}s so that they respond to touch.
 *
 * @param input
 * @param linkTextColor
 * @param linkHighlightColor
 * @return
 */
public static Spanned parseHtml(String input,
                                ColorStateList linkTextColor,
                                @ColorInt int linkHighlightColor) {
    SpannableStringBuilder spanned = (SpannableStringBuilder) Html.fromHtml(input);

    // strip any trailing newlines
    while (spanned.charAt(spanned.length() - 1) == '\n') {
        spanned = spanned.delete(spanned.length() - 1, spanned.length());
    }

    URLSpan[] urlSpans = spanned.getSpans(0, spanned.length(), URLSpan.class);
    for (URLSpan urlSpan : urlSpans) {
        int start = spanned.getSpanStart(urlSpan);
        int end = spanned.getSpanEnd(urlSpan);
        spanned.removeSpan(urlSpan);
        // spanned.subSequence(start, start + 1) == "@" TODO send to our own user activity...
        // when i've written it
        spanned.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor,
                linkHighlightColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return spanned;
}
 
Example #8
Source File: ConverterSpannedToHtml.java    From memoir with Apache License 2.0 6 votes vote down vote up
private void handleEndTag(CharacterStyle style) {
    if (style instanceof URLSpan) {
        mOut.append("</a>");
    } else if (style instanceof TypefaceSpan) {
        mOut.append("</font>");
    } else if (style instanceof ForegroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof BackgroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof AbsoluteSizeSpan) {
        mOut.append("</font>");
    } else if (style instanceof StrikethroughSpan) {
        mOut.append("</strike>");
    } else if (style instanceof SubscriptSpan) {
        mOut.append("</sub>");
    } else if (style instanceof SuperscriptSpan) {
        mOut.append("</sup>");
    } else if (style instanceof UnderlineSpan) {
        mOut.append("</u>");
    } else if (style instanceof BoldSpan) {
        mOut.append("</b>");
    } else if (style instanceof ItalicSpan) {
        mOut.append("</i>");
    }
}
 
Example #9
Source File: TextViewLinkHandler.java    From ResearchStack with Apache License 2.0 6 votes vote down vote up
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_UP) {
        return super.onTouchEvent(widget, buffer, event);
    }

    int x = (int) event.getX();
    int y = (int) event.getY();

    x -= widget.getTotalPaddingLeft();
    y -= widget.getTotalPaddingTop();

    x += widget.getScrollX();
    y += widget.getScrollY();

    Layout layout = widget.getLayout();
    int line = layout.getLineForVertical(y);
    int off = layout.getOffsetForHorizontal(line, x);

    URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
    if (link.length != 0) {
        onLinkClick(link[0].getURL());
    }
    return true;
}
 
Example #10
Source File: Html.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Href h = (Href) obj;

        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example #11
Source File: MessageFormatter.java    From talk-android with MIT License 6 votes vote down vote up
public static Spannable formatURLSpan(Spannable s) {
    Linkify.addLinks(s, Linkify.WEB_URLS);
    URLSpan[] urlSpans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan urlSpan : urlSpans) {
        final String url = urlSpan.getURL();
        final Matcher m = chinesePattern.matcher(url);
        if (m.find()) {
            s.removeSpan(urlSpan);
            continue;
        }
        int start = s.getSpanStart(urlSpan);
        int end = s.getSpanEnd(urlSpan);
        s.removeSpan(urlSpan);
        s.setSpan(new TalkURLSpan(urlSpan.getURL(), ThemeUtil.getThemeColor(MainApp.CONTEXT.
                        getResources(), BizLogic.getTeamColor())), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return s;
}
 
Example #12
Source File: GalleryCommentsScene.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) {
    Activity activity = getActivity2();
    if (null == activity) {
        return false;
    }

    RecyclerView.ViewHolder holder = parent.getChildViewHolder(view);
    if (holder instanceof CommentHolder) {
        CommentHolder commentHolder = (CommentHolder) holder;
        ClickableSpan span = commentHolder.comment.getCurrentSpan();
        commentHolder.comment.clearCurrentSpan();

        if (span instanceof URLSpan) {
            UrlOpener.openUrl(activity, ((URLSpan) span).getURL(), true);
            return true;
        }
    }

    showCommentDialog(position);

    return true;
}
 
Example #13
Source File: KnifeText.java    From Knife with Apache License 2.0 6 votes vote down vote up
protected boolean containLink(int start, int end) {
    if (start > end) {
        return false;
    }

    if (start == end) {
        if (start - 1 < 0 || start + 1 > getEditableText().length()) {
            return false;
        } else {
            URLSpan[] before = getEditableText().getSpans(start - 1, start, URLSpan.class);
            URLSpan[] after = getEditableText().getSpans(start, start + 1, URLSpan.class);
            return before.length > 0 && after.length > 0;
        }
    } else {
        StringBuilder builder = new StringBuilder();

        for (int i = start; i < end; i++) {
            if (getEditableText().getSpans(i, i + 1, URLSpan.class).length > 0) {
                builder.append(getEditableText().subSequence(i, i + 1).toString());
            }
        }

        return getEditableText().subSequence(start, end).toString().equals(builder.toString());
    }
}
 
Example #14
Source File: HtmlUtils.java    From android-proguards with Apache License 2.0 6 votes vote down vote up
private static SpannableStringBuilder linkifyPlainLinks(
        CharSequence input,
        ColorStateList linkTextColor,
        @ColorInt int linkHighlightColor) {
    final SpannableString plainLinks = new SpannableString(input); // copy of input

    // Linkify doesn't seem to work as expected on M+
    // TODO: figure out why
    //Linkify.addLinks(plainLinks, Linkify.WEB_URLS);

    final URLSpan[] urlSpans = plainLinks.getSpans(0, plainLinks.length(), URLSpan.class);

    // add any plain links to the output
    final SpannableStringBuilder ssb = new SpannableStringBuilder(input);
    for (URLSpan urlSpan : urlSpans) {
        ssb.removeSpan(urlSpan);
        ssb.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor),
                plainLinks.getSpanStart(urlSpan),
                plainLinks.getSpanEnd(urlSpan),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return ssb;
}
 
Example #15
Source File: DiscussionTextUtils.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Renders various HTML elements and plain hyperlinks in the given HTML to clickable items
 * while applying it on the given {@link TextView}.
 *
 * @param textView The {@link TextView} which will render the given HTML.
 * @param html     The HTML to render.
 */
public static void renderHtml(@NonNull TextView textView, @NonNull String html) {
    Spanned spannedHtml = DiscussionTextUtils.parseHtml(html);
    URLSpan[] urlSpans = spannedHtml.getSpans(0, spannedHtml.length(), URLSpan.class);
    textView.setAutoLinkMask(Linkify.ALL);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(spannedHtml);

    SpannableString viewText = (SpannableString) textView.getText();
    for (final URLSpan spanObj : urlSpans) {
        final int start = spannedHtml.getSpanStart(spanObj);
        final int end = spannedHtml.getSpanEnd(spanObj);
        final int flags = spannedHtml.getSpanFlags(spanObj);
        viewText.setSpan(spanObj, start, end, flags);
    }
}
 
Example #16
Source File: IMChatBaseView.java    From imsdk-android with MIT License 6 votes vote down vote up
private void handleUrlSpan() {
    mTimeTextView.setMovementMethod(LinkMovementMethod.getInstance());
    CharSequence text = mTimeTextView.getText();
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
    if (text instanceof Spannable) {
        int end = text.length();
        Spannable spannable = (Spannable) mTimeTextView.getText();
        URLSpan[] urlSpans = spannable.getSpans(0, end, URLSpan.class);
        if (urlSpans.length == 0) {
            return;
        }
        // 循环遍历并拦截
        for (URLSpan uri : urlSpans) {
            String url = uri.getURL();
            if (url.indexOf("http://") == 0 || url.indexOf("https://") == 0) {
                CustomUrlSpan customUrlSpan = new CustomUrlSpan(context, url);
                spannableStringBuilder.setSpan(customUrlSpan, spannable.getSpanStart(uri),
                        spannable.getSpanEnd(uri), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            spannableStringBuilder.removeSpan(uri);
        }
        mTimeTextView.setText(spannableStringBuilder);
    }
}
 
Example #17
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 #18
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 #19
Source File: StringItem.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
@Override
public void process() {
	if (appearanceMode == HYPERLINK && text != null) {
		SpannableStringBuilder s = new SpannableStringBuilder(text);
		s.setSpan(new URLSpan(text), 0, s.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		textview.setText(s);
	} else {
		textview.setText(text);
	}
}
 
Example #20
Source File: RuleSpannables.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a menu item for URLSpan. <strong>Note: </strong> This method will not create menu item
 * for relative URLs.
 */
private @Nullable ContextMenuItem createMenuItemForUrlSpan(
    Context context,
    ContextMenuItemBuilder menuItemBuilder,
    int itemId,
    Spannable spannable,
    URLSpan span) {
  final String url = span.getURL();
  final int start = spannable.getSpanStart(span);
  final int end = spannable.getSpanEnd(span);
  if (start < 0 || end < 0) {
    return null;
  }
  final CharSequence label = spannable.subSequence(start, end);
  if (TextUtils.isEmpty(url) || TextUtils.isEmpty(label)) {
    return null;
  }

  final Uri uri = Uri.parse(url);
  if (uri.isRelative()) {
    // Generally, only absolute URIs are resolvable to an activity
    return null;
  }

  // Strip out ClickableSpans/UrlSpans from the label text.
  // A11y framework has changed how it handles double-tap from O. It's possible that double-tap
  // on the menu item will invoke ClickableSpans in the label text instead of calling
  // MenuItemClickListener. Thus we should remove ClickableSpans from label text.
  // Also apply this rule to pre-O in order to have consistent text appearance.
  SpannableUtils.stripTargetSpanFromText(label, TARGET_SPAN_CLASS);
  final ContextMenuItem item =
      menuItemBuilder.createMenuItem(context, R.id.group_links, itemId, Menu.NONE, label);
  item.setOnMenuItemClickListener(new UrlSpanMenuItemClickListener(context, uri));
  return item;
}
 
Example #21
Source File: ClickableURLSpan.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
public static ClickableURLSpan replaceURLSpan(SpannableStringBuilder builder, URLSpan span, int color) {
    int start = builder.getSpanStart(span);
    int end = builder.getSpanEnd(span);
    String url = span.getURL();
    
    builder.removeSpan(span);
    
    ClickableURLSpan newSpan = new ClickableURLSpan(url);
    builder.setSpan(newSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    return newSpan;
}
 
Example #22
Source File: MessageFormatter.java    From talk-android with MIT License 5 votes vote down vote up
public static Spannable formatURLSpan(String str) {
    Spannable s = new SpannableStringBuilder(str);
    Linkify.addLinks(s, Linkify.WEB_URLS);
    URLSpan[] urlSpans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan urlSpan : urlSpans) {
        int start = s.getSpanStart(urlSpan);
        int end = s.getSpanEnd(urlSpan);
        s.removeSpan(urlSpan);
        s.setSpan(new TalkURLSpan(urlSpan.getURL(), ThemeUtil.getThemeColor(MainApp.CONTEXT.
                        getResources(), BizLogic.getTeamColor())), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return s;
}
 
Example #23
Source File: Utils.java    From openshop.io-android with MIT License 5 votes vote down vote up
/**
 * 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 #24
Source File: SpannableStringUtils.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
public static boolean hasUrlSpans(final CharSequence text,
        final int startIndex, final int endIndex) {
    if (!(text instanceof Spanned)) {
        return false; // Not spanned, so no link
    }
    final Spanned spanned = (Spanned)text;
    // getSpans(x, y) does not return spans that start on x or end on y. x-1, y+1 does the
    // trick, and works in all cases even if startIndex <= 0 or endIndex >= text.length().
    final URLSpan[] spans = spanned.getSpans(startIndex - 1, endIndex + 1, URLSpan.class);
    return null != spans && spans.length > 0;
}
 
Example #25
Source File: AnnouncementBodyAdapter.java    From PKUCourses with GNU General Public License v3.0 5 votes vote down vote up
private void setTextViewHTML(TextView text, String html) {
    CharSequence sequence = Html.fromHtml(html.replaceAll("\n", ""));
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }
    text.setText(strBuilder);
    text.setMovementMethod(LinkMovementMethod.getInstance());
}
 
Example #26
Source File: HtmlParser.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Href h = (Href) obj;

        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example #27
Source File: AlertsCreator.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public static AlertDialog createSupportAlert(BaseFragment fragment) {
    if (fragment == null || fragment.getParentActivity() == null) {
        return null;
    }
    final TextView message = new TextView(fragment.getParentActivity());
    Spannable spanned = new SpannableString(Html.fromHtml(LocaleController.getString("AskAQuestionInfo", R.string.AskAQuestionInfo).replace("\n", "<br>")));
    URLSpan[] spans = spanned.getSpans(0, spanned.length(), URLSpan.class);
    for (int i = 0; i < spans.length; i++) {
        URLSpan span = spans[i];
        int start = spanned.getSpanStart(span);
        int end = spanned.getSpanEnd(span);
        spanned.removeSpan(span);
        span = new URLSpanNoUnderline(span.getURL()) {
            @Override
            public void onClick(View widget) {
                fragment.dismissCurrentDialig();
                super.onClick(widget);
            }
        };
        spanned.setSpan(span, start, end, 0);
    }
    message.setText(spanned);
    message.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    message.setLinkTextColor(Theme.getColor(Theme.key_dialogTextLink));
    message.setHighlightColor(Theme.getColor(Theme.key_dialogLinkSelection));
    message.setPadding(AndroidUtilities.dp(23), 0, AndroidUtilities.dp(23), 0);
    message.setMovementMethod(new AndroidUtilities.LinkMovementMethodMy());
    message.setTextColor(Theme.getColor(Theme.key_dialogTextBlack));

    AlertDialog.Builder builder1 = new AlertDialog.Builder(fragment.getParentActivity());
    builder1.setView(message);
    builder1.setTitle(LocaleController.getString("AskAQuestion", R.string.AskAQuestion));
    builder1.setPositiveButton(LocaleController.getString("AskButton", R.string.AskButton), (dialogInterface, i) -> performAskAQuestion(fragment));
    builder1.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
    return builder1.create();
}
 
Example #28
Source File: TimeLineUtility.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
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 #29
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 #30
Source File: MainActivity.java    From YCBanner with Apache License 2.0 5 votes vote down vote up
public ArrayList<String> getMarqueeTitle() {
    ArrayList<String> list = new ArrayList<>();
    String[] title = getResources().getStringArray(R.array.main_marquee_title);
    SpannableString ss1 = new SpannableString(title[0]);
    ss1.setSpan(new ForegroundColorSpan(Color.BLACK),  2, title[0].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    list.add(ss1.toString());
    SpannableString ss2 = new SpannableString(title[1]);
    ss2.setSpan(new ForegroundColorSpan(Color.BLACK),  2, title[1].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    list.add(ss2.toString());
    SpannableString ss3 = new SpannableString(title[2]);
    ss3.setSpan(new URLSpan("http://www.ximalaya.com/zhubo/71989305/"), 2, title[2].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    list.add(ss3.toString());
    return list;
}