Search in sources :

Example 96 with ClickableSpan

use of android.text.style.ClickableSpan in project Klyph by jonathangerbaud.

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) {
                link[0].onClick(widget);
            } else if (action == MotionEvent.ACTION_DOWN) {
            /*Selection.setSelection(buffer,
                                           buffer.getSpanStart(link[0]),
                                           buffer.getSpanEnd(link[0]));*/
            }
            return false;
        } else {
        // Selection.removeSelection(buffer);
        }
    }
    // return super.onTouchEvent(widget, buffer, event);
    return false;
}
Also used : Layout(android.text.Layout) ClickableSpan(android.text.style.ClickableSpan)

Example 97 with ClickableSpan

use of android.text.style.ClickableSpan in project wire-android by wireapp.

the class TextViewUtils method linkifyText.

public static void linkifyText(TextView textView, final int highlightColor, int boldTypeface, final boolean underline, final Runnable onClick) {
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    final String string = textView.getText().toString();
    final int highlightStart = string.indexOf('_');
    if (highlightStart < 0) {
        Logger.error(TAG, "Failed to highlight text - could not find _ marker in string.");
        return;
    }
    final int highlightEnd = string.lastIndexOf('_') - 1;
    if (highlightStart >= highlightEnd) {
        Logger.error(TAG, "Failed to highlight text - make sure you have 2 _ markers to denote start and end of highlight region");
        return;
    }
    final SpannableStringBuilder str = new SpannableStringBuilder(textView.getText());
    str.replace(highlightStart, (highlightStart + 1), "");
    str.replace(highlightEnd, (highlightEnd + 1), "");
    final Typeface typeface = textView.getTypeface();
    ClickableSpan linkSpan = new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            if (onClick == null) {
                return;
            }
            onClick.run();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(underline);
            ds.setTypeface(typeface);
            ds.setColor(highlightColor);
        }
    };
    str.setSpan(linkSpan, highlightStart, highlightEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    if (boldTypeface > 0) {
        str.setSpan(new CustomTypefaceSpan("", textView.getResources().getString(boldTypeface)), highlightStart, highlightEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    textView.setText(str);
}
Also used : Typeface(android.graphics.Typeface) SpannableString(android.text.SpannableString) ClickableSpan(android.text.style.ClickableSpan) TextView(android.widget.TextView) View(android.view.View) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SpannableStringBuilder(android.text.SpannableStringBuilder) TextPaint(android.text.TextPaint)

Example 98 with ClickableSpan

use of android.text.style.ClickableSpan in project orgzly-android by orgzly.

the class EspressoUtils method clickClickableSpan.

public static ViewAction clickClickableSpan(final CharSequence textToClick) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return Matchers.instanceOf(TextView.class);
        }

        @Override
        public String getDescription() {
            return "Click text";
        }

        @Override
        public void perform(UiController uiController, View view) {
            TextView textView = (TextView) view;
            Spanned spannable = (Spanned) textView.getText();
            ClickableSpan clickable = null;
            for (ClickableSpan span : SpanUtils.getSpans(spannable, ClickableSpan.class)) {
                int start = spannable.getSpanStart(span);
                int end = spannable.getSpanEnd(span);
                CharSequence sequence = spannable.subSequence(start, end);
                if (sequence.toString().contains(textToClick)) {
                    clickable = span;
                    break;
                }
            }
            if (clickable != null) {
                clickable.onClick(textView);
            } else {
                throw new IllegalStateException("No clickable span found in " + spannable);
            }
        }
    };
}
Also used : ViewAction(androidx.test.espresso.ViewAction) UiController(androidx.test.espresso.UiController) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) Espresso.onView(androidx.test.espresso.Espresso.onView) TextView(android.widget.TextView) ListView(android.widget.ListView) Spanned(android.text.Spanned) ClickableSpan(android.text.style.ClickableSpan) ViewMatchers.withHint(androidx.test.espresso.matcher.ViewMatchers.withHint)

Example 99 with ClickableSpan

use of android.text.style.ClickableSpan in project AndroidChromium by JackyAndroid.

the class PassphraseTypeDialogFragment method getResetText.

private SpannableString getResetText() {
    final Context context = getActivity();
    return SpanApplier.applySpans(context.getString(R.string.sync_passphrase_encryption_reset_instructions), new SpanInfo("<resetlink>", "</resetlink>", new ClickableSpan() {

        @Override
        public void onClick(View view) {
            Uri syncDashboardUrl = Uri.parse(context.getText(R.string.sync_dashboard_url).toString());
            Intent intent = new Intent(Intent.ACTION_VIEW, syncDashboardUrl);
            intent.setPackage(BuildInfo.getPackageName(context));
            IntentUtils.safePutBinderExtra(intent, CustomTabsIntent.EXTRA_SESSION, null);
            context.startActivity(intent);
        }
    }));
}
Also used : Context(android.content.Context) SpanInfo(org.chromium.ui.text.SpanApplier.SpanInfo) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) ClickableSpan(android.text.style.ClickableSpan) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) CheckedTextView(android.widget.CheckedTextView) ListView(android.widget.ListView) Uri(android.net.Uri)

Example 100 with ClickableSpan

use of android.text.style.ClickableSpan in project AndroidChromium by JackyAndroid.

the class PassphraseDialogFragment method getResetText.

private SpannableString getResetText() {
    final Context context = getActivity();
    return SpanApplier.applySpans(context.getString(R.string.sync_passphrase_reset_instructions), new SpanInfo("<resetlink>", "</resetlink>", new ClickableSpan() {

        @Override
        public void onClick(View view) {
            recordPassphraseDialogDismissal(PASSPHRASE_DIALOG_RESET_LINK);
            Uri syncDashboardUrl = Uri.parse(context.getText(R.string.sync_dashboard_url).toString());
            Intent intent = new Intent(Intent.ACTION_VIEW, syncDashboardUrl);
            intent.setPackage(BuildInfo.getPackageName(context));
            IntentUtils.safePutBinderExtra(intent, CustomTabsIntent.EXTRA_SESSION, null);
            context.startActivity(intent);
        }
    }));
}
Also used : Context(android.content.Context) SpanInfo(org.chromium.ui.text.SpanApplier.SpanInfo) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) ClickableSpan(android.text.style.ClickableSpan) View(android.view.View) TextView(android.widget.TextView) Uri(android.net.Uri)

Aggregations

ClickableSpan (android.text.style.ClickableSpan)133 View (android.view.View)76 TextView (android.widget.TextView)55 TextPaint (android.text.TextPaint)50 SpannableString (android.text.SpannableString)46 Layout (android.text.Layout)37 Spannable (android.text.Spannable)28 SpannableStringBuilder (android.text.SpannableStringBuilder)24 Intent (android.content.Intent)17 NonNull (androidx.annotation.NonNull)15 Paint (android.graphics.Paint)14 Context (android.content.Context)12 Spanned (android.text.Spanned)12 URLSpan (android.text.style.URLSpan)12 ImageView (android.widget.ImageView)12 StyleSpan (android.text.style.StyleSpan)9 Uri (android.net.Uri)8 SuppressLint (android.annotation.SuppressLint)7 InputMethodManager (android.view.inputmethod.InputMethodManager)7 DialogInterface (android.content.DialogInterface)6