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;
}
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);
}
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);
}
}
};
}
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);
}
}));
}
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);
}
}));
}
Aggregations