Search in sources :

Example 41 with URLSpan

use of android.text.style.URLSpan in project ForPDA by RadiationX.

the class LinkMovementMethod method onTouchEvent.

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();
    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
        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);
        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
        if (link.length != 0) {
            if (action == MotionEvent.ACTION_UP) {
                String url = ((URLSpan) link[0]).getURL();
                if (!IntentHandler.handle(url))
                    link[0].onClick(widget);
            } else {
                Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
            }
            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }
    return super.onTouchEvent(widget, buffer, event);
}
Also used : Layout(android.text.Layout) URLSpan(android.text.style.URLSpan) ClickableSpan(android.text.style.ClickableSpan)

Example 42 with URLSpan

use of android.text.style.URLSpan in project Osmand by osmandapp.

the class AndroidUtils method removeLinkUnderline.

public static void removeLinkUnderline(TextView textView) {
    Spannable s = new SpannableString(textView.getText());
    for (URLSpan span : s.getSpans(0, s.length(), URLSpan.class)) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);
        span = new URLSpan(span.getURL()) {

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        };
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}
Also used : SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) Spannable(android.text.Spannable) SuppressLint(android.annotation.SuppressLint) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint)

Example 43 with URLSpan

use of android.text.style.URLSpan in project Tusky by Vavassor.

the class LinkHelper method setClickableText.

/**
 * Finds links, mentions, and hashtags in a piece of text and makes them clickable, associating
 * them with callbacks to notify when they're clicked.
 *
 * @param view the returned text will be put in
 * @param content containing text with mentions, links, or hashtags
 * @param mentions any '@' mentions which are known to be in the content
 * @param listener to notify about particular spans that are clicked
 */
public static void setClickableText(TextView view, CharSequence content, @Nullable List<Status.Mention> mentions, final LinkListener listener) {
    SpannableStringBuilder builder = SpannableStringBuilder.valueOf(content);
    URLSpan[] urlSpans = builder.getSpans(0, content.length(), URLSpan.class);
    for (URLSpan span : urlSpans) {
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        int flags = builder.getSpanFlags(span);
        CharSequence text = builder.subSequence(start, end);
        ClickableSpan customSpan = null;
        if (text.charAt(0) == '#') {
            final String tag = text.subSequence(1, text.length()).toString();
            customSpan = new NoUnderlineURLSpan(span.getURL()) {

                @Override
                public void onClick(@NonNull View widget) {
                    listener.onViewTag(tag);
                }
            };
        } else if (text.charAt(0) == '@' && mentions != null && mentions.size() > 0) {
            // https://github.com/tuskyapp/Tusky/pull/2339
            String id = null;
            for (Status.Mention mention : mentions) {
                if (mention.getUrl().equals(span.getURL())) {
                    id = mention.getId();
                    break;
                }
            }
            if (id != null) {
                final String accountId = id;
                customSpan = new NoUnderlineURLSpan(span.getURL()) {

                    @Override
                    public void onClick(@NonNull View widget) {
                        listener.onViewAccount(accountId);
                    }
                };
            }
        }
        if (customSpan == null) {
            customSpan = new NoUnderlineURLSpan(span.getURL()) {

                @Override
                public void onClick(@NonNull View widget) {
                    listener.onViewUrl(getURL());
                }
            };
        }
        builder.removeSpan(span);
        builder.setSpan(customSpan, start, end, flags);
        /* Add zero-width space after links in end of line to fix its too large hitbox.
             * See also : https://github.com/tuskyapp/Tusky/issues/846
             *            https://github.com/tuskyapp/Tusky/pull/916 */
        if (end >= builder.length() || builder.subSequence(end, end + 1).toString().equals("\n")) {
            builder.insert(end, "\u200B");
        }
    }
    view.setText(builder);
    view.setMovementMethod(LinkMovementMethod.getInstance());
}
Also used : NonNull(androidx.annotation.NonNull) URLSpan(android.text.style.URLSpan) ClickableSpan(android.text.style.ClickableSpan) TextView(android.widget.TextView) View(android.view.View) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 44 with URLSpan

use of android.text.style.URLSpan in project Tusky by Vavassor.

the class LinkHelper method createClickableText.

public static CharSequence createClickableText(String text, String link) {
    URLSpan span = new NoUnderlineURLSpan(link);
    SpannableStringBuilder clickableText = new SpannableStringBuilder(text);
    clickableText.setSpan(span, 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    return clickableText;
}
Also used : URLSpan(android.text.style.URLSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 45 with URLSpan

use of android.text.style.URLSpan in project Android-Terminal-Emulator by jackpal.

the class EmulatorView method getURLat.

/**
 * Get the URL for the link displayed at the specified screen coordinates.
 *
 * @param x The x coordinate being queried (from 0 to screen width)
 * @param y The y coordinate being queried (from 0 to screen height)
 * @return The URL for the link at the specified screen coordinates, or
 *         null if no link exists there.
 */
public String getURLat(float x, float y) {
    float w = getWidth();
    float h = getHeight();
    // If width or height is zero, there are probably no links around, so return null.
    if (w == 0 || h == 0)
        return null;
    // Get fraction of total screen
    float x_pos = x / w;
    float y_pos = y / h;
    // Convert to integer row/column index
    int row = (int) Math.floor(y_pos * mRows);
    int col = (int) Math.floor(x_pos * mColumns);
    // Grab row from link layer
    URLSpan[] linkRow = mLinkLayer.get(row);
    URLSpan link;
    // If row exists, and link exists at column, return it
    if (linkRow != null && (link = linkRow[col]) != null)
        return link.getURL();
    else
        return null;
}
Also used : URLSpan(android.text.style.URLSpan) Paint(android.graphics.Paint)

Aggregations

URLSpan (android.text.style.URLSpan)134 SpannableString (android.text.SpannableString)36 Spannable (android.text.Spannable)22 SpannableStringBuilder (android.text.SpannableStringBuilder)21 TextPaint (android.text.TextPaint)21 Spanned (android.text.Spanned)20 TextView (android.widget.TextView)19 ForegroundColorSpan (android.text.style.ForegroundColorSpan)16 StyleSpan (android.text.style.StyleSpan)16 UnderlineSpan (android.text.style.UnderlineSpan)14 Paint (android.graphics.Paint)13 ImageSpan (android.text.style.ImageSpan)13 StrikethroughSpan (android.text.style.StrikethroughSpan)13 TypefaceSpan (android.text.style.TypefaceSpan)13 View (android.view.View)13 Intent (android.content.Intent)12 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)12 BackgroundColorSpan (android.text.style.BackgroundColorSpan)12 ClickableSpan (android.text.style.ClickableSpan)12 SubscriptSpan (android.text.style.SubscriptSpan)12