Search in sources :

Example 1 with ClickableSpan

use of android.text.style.ClickableSpan in project Signal-Android by WhisperSystems.

the class RegistrationProgressActivity method initializeLinks.

private void initializeLinks() {
    TextView failureText = (TextView) findViewById(R.id.sms_failed_text);
    String pretext = getString(R.string.registration_progress_activity__signal_timed_out_while_waiting_for_a_verification_sms_message);
    String link = getString(R.string.RegistrationProblemsActivity_possible_problems);
    SpannableString spannableString = new SpannableString(pretext + " " + link);
    spannableString.setSpan(new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            new AlertDialog.Builder(RegistrationProgressActivity.this).setTitle(R.string.RegistrationProblemsActivity_possible_problems).setView(R.layout.registration_problems).setNeutralButton(android.R.string.ok, null).show();
        }
    }, pretext.length() + 1, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    failureText.setText(spannableString);
    failureText.setMovementMethod(LinkMovementMethod.getInstance());
}
Also used : SpannableString(android.text.SpannableString) AlertDialog(android.support.v7.app.AlertDialog) TextView(android.widget.TextView) SpannableString(android.text.SpannableString) ClickableSpan(android.text.style.ClickableSpan) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Example 2 with ClickableSpan

use of android.text.style.ClickableSpan in project actor-platform by actorapp.

the class AndroidMarkdown method processText.

private static Spannable processText(String markdown, int mode) {
    MDDocument doc = new MarkdownParser(mode).processDocument(markdown);
    SpannableStringBuilder builder = new SpannableStringBuilder();
    boolean isFirst = true;
    for (MDSection s : doc.getSections()) {
        if (isFirst) {
            isFirst = false;
        } else {
            builder.append("\n");
        }
        if (s.getType() == MDSection.TYPE_CODE) {
            int start = builder.length();
            builder.append("View Source Code");
            final String text = s.getCode().getCode();
            builder.setSpan(new RelativeSizeSpan(1.1f), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.setSpan(new ForegroundColorSpan(Color.RED), start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View view) {
                    AndroidContext.getContext().startActivity(new Intent(AndroidContext.getContext(), CodePreviewActivity.class).putExtra("source_code", text).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
            }, start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (s.getType() == MDSection.TYPE_TEXT) {
            writeText(s.getText(), builder);
        } else {
            throw new RuntimeException("Unknown section type: " + s.getType());
        }
    }
    return builder;
}
Also used : MDSection(im.actor.runtime.markdown.MDSection) ForegroundColorSpan(android.text.style.ForegroundColorSpan) MDDocument(im.actor.runtime.markdown.MDDocument) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RelativeSizeSpan(android.text.style.RelativeSizeSpan) ClickableSpan(android.text.style.ClickableSpan) View(android.view.View) MarkdownParser(im.actor.runtime.markdown.MarkdownParser) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 3 with ClickableSpan

use of android.text.style.ClickableSpan in project XobotOS by xamarin.

the class LinkMovementMethod method action.

private boolean action(int what, TextView widget, Spannable buffer) {
    Layout layout = widget.getLayout();
    int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
    int areatop = widget.getScrollY();
    int areabot = areatop + widget.getHeight() - padding;
    int linetop = layout.getLineForVertical(areatop);
    int linebot = layout.getLineForVertical(areabot);
    int first = layout.getLineStart(linetop);
    int last = layout.getLineEnd(linebot);
    ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);
    int a = Selection.getSelectionStart(buffer);
    int b = Selection.getSelectionEnd(buffer);
    int selStart = Math.min(a, b);
    int selEnd = Math.max(a, b);
    if (selStart < 0) {
        if (buffer.getSpanStart(FROM_BELOW) >= 0) {
            selStart = selEnd = buffer.length();
        }
    }
    if (selStart > last)
        selStart = selEnd = Integer.MAX_VALUE;
    if (selEnd < first)
        selStart = selEnd = -1;
    switch(what) {
        case CLICK:
            if (selStart == selEnd) {
                return false;
            }
            ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);
            if (link.length != 1)
                return false;
            link[0].onClick(widget);
            break;
        case UP:
            int beststart, bestend;
            beststart = -1;
            bestend = -1;
            for (int i = 0; i < candidates.length; i++) {
                int end = buffer.getSpanEnd(candidates[i]);
                if (end < selEnd || selStart == selEnd) {
                    if (end > bestend) {
                        beststart = buffer.getSpanStart(candidates[i]);
                        bestend = end;
                    }
                }
            }
            if (beststart >= 0) {
                Selection.setSelection(buffer, bestend, beststart);
                return true;
            }
            break;
        case DOWN:
            beststart = Integer.MAX_VALUE;
            bestend = Integer.MAX_VALUE;
            for (int i = 0; i < candidates.length; i++) {
                int start = buffer.getSpanStart(candidates[i]);
                if (start > selStart || selStart == selEnd) {
                    if (start < beststart) {
                        beststart = start;
                        bestend = buffer.getSpanEnd(candidates[i]);
                    }
                }
            }
            if (bestend < Integer.MAX_VALUE) {
                Selection.setSelection(buffer, beststart, bestend);
                return true;
            }
            break;
    }
    return false;
}
Also used : Layout(android.text.Layout) ClickableSpan(android.text.style.ClickableSpan)

Example 4 with ClickableSpan

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

the class LinkHelper method setClickableText.

public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions, boolean useCustomTabs, final LinkListener listener) {
    SpannableStringBuilder builder = new SpannableStringBuilder(content);
    URLSpan[] urlSpans = content.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);
        if (text.charAt(0) == '#') {
            final String tag = text.subSequence(1, text.length()).toString();
            ClickableSpan newSpan = new ClickableSpan() {

                @Override
                public void onClick(View widget) {
                    listener.onViewTag(tag);
                }
            };
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        } else if (text.charAt(0) == '@' && mentions != null) {
            final String accountUsername = text.subSequence(1, text.length()).toString();
            String id = null;
            for (Status.Mention mention : mentions) {
                if (mention.localUsername.equals(accountUsername)) {
                    id = mention.id;
                }
            }
            if (id != null) {
                final String accountId = id;
                ClickableSpan newSpan = new ClickableSpan() {

                    @Override
                    public void onClick(View widget) {
                        listener.onViewAccount(accountId);
                    }
                };
                builder.removeSpan(span);
                builder.setSpan(newSpan, start, end, flags);
            }
        } else if (useCustomTabs) {
            ClickableSpan newSpan = new CustomTabURLSpan(span.getURL());
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        }
    }
    view.setText(builder);
    view.setLinksClickable(true);
    view.setMovementMethod(LinkMovementMethod.getInstance());
}
Also used : URLSpan(android.text.style.URLSpan) ClickableSpan(android.text.style.ClickableSpan) TextView(android.widget.TextView) View(android.view.View) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 5 with ClickableSpan

use of android.text.style.ClickableSpan in project Rocket.Chat.Android by RocketChat.

the class LinkMovementMethodCompat method onTouchEvent.

// http://stackoverflow.com/a/30572151/2104686
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();
    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
        int eventX = (int) event.getX();
        int eventY = (int) event.getY();
        eventX -= widget.getTotalPaddingLeft();
        eventY -= widget.getTotalPaddingTop();
        eventX += widget.getScrollX();
        eventY += widget.getScrollY();
        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(eventY);
        int off = layout.getOffsetForHorizontal(line, eventX);
        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
        if (link.length != 0) {
            if (action == MotionEvent.ACTION_UP) {
                link[0].onClick(widget);
            } else {
                Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
            }
            return true;
        }
    }
    return false;
}
Also used : Layout(android.text.Layout) ClickableSpan(android.text.style.ClickableSpan)

Aggregations

ClickableSpan (android.text.style.ClickableSpan)118 View (android.view.View)63 TextView (android.widget.TextView)46 TextPaint (android.text.TextPaint)43 Layout (android.text.Layout)36 SpannableString (android.text.SpannableString)35 Spannable (android.text.Spannable)25 SpannableStringBuilder (android.text.SpannableStringBuilder)22 Intent (android.content.Intent)15 Paint (android.graphics.Paint)14 URLSpan (android.text.style.URLSpan)11 Context (android.content.Context)10 Spanned (android.text.Spanned)9 StyleSpan (android.text.style.StyleSpan)9 NonNull (androidx.annotation.NonNull)9 Uri (android.net.Uri)8 InputMethodManager (android.view.inputmethod.InputMethodManager)7 ImageView (android.widget.ImageView)7 SuppressLint (android.annotation.SuppressLint)6 DialogInterface (android.content.DialogInterface)6