Java Code Examples for android.text.style.ClickableSpan#onClick()

The following examples show how to use android.text.style.ClickableSpan#onClick() . 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: AccessibilityInteractionController.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private boolean handleClickableSpanActionUiThread(
        View view, int virtualDescendantId, Bundle arguments) {
    Parcelable span = arguments.getParcelable(ACTION_ARGUMENT_ACCESSIBLE_CLICKABLE_SPAN);
    if (!(span instanceof AccessibilityClickableSpan)) {
        return false;
    }

    // Find the original ClickableSpan if it's still on the screen
    AccessibilityNodeInfo infoWithSpan = null;
    AccessibilityNodeProvider provider = view.getAccessibilityNodeProvider();
    if (provider != null) {
        infoWithSpan = provider.createAccessibilityNodeInfo(virtualDescendantId);
    } else if (virtualDescendantId == AccessibilityNodeProvider.HOST_VIEW_ID) {
        infoWithSpan = view.createAccessibilityNodeInfo();
    }
    if (infoWithSpan == null) {
        return false;
    }

    // Click on the corresponding span
    ClickableSpan clickableSpan = ((AccessibilityClickableSpan) span).findClickableSpan(
            infoWithSpan.getOriginalText());
    if (clickableSpan != null) {
        clickableSpan.onClick(view);
        return true;
    }
    return false;
}
 
Example 2
Source File: ClickableSpanEZTest.java    From SpanEZ with Apache License 2.0 5 votes vote down vote up
@Test
public void clicking_the_span_trigger_the_callback() {
    ClickableSpan clickableSpanEZ = ClickableSpanEZ.from(mockListener, FAKE_CONTENT);

    clickableSpanEZ.onClick(null);
    verify(mockListener, times(1)).onSpanClick(FAKE_CONTENT);
}
 
Example 3
Source File: LinkMovementMethod.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@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[] links = buffer.getSpans(off, off, ClickableSpan.class);

        if (links.length != 0) {
            ClickableSpan link = links[0];
            if (action == MotionEvent.ACTION_UP) {
                if (link instanceof TextLinkSpan) {
                    ((TextLinkSpan) link).onClick(
                            widget, TextLinkSpan.INVOCATION_METHOD_TOUCH);
                } else {
                    link.onClick(widget);
                }
            } else if (action == MotionEvent.ACTION_DOWN) {
                if (widget.getContext().getApplicationInfo().targetSdkVersion
                        >= Build.VERSION_CODES.P) {
                    // Selection change will reposition the toolbar. Hide it for a few ms for a
                    // smoother transition.
                    widget.hideFloatingToolbar(HIDE_FLOATING_TOOLBAR_DELAY_MS);
                }
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link),
                        buffer.getSpanEnd(link));
            }
            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }

    return super.onTouchEvent(widget, buffer, event);
}
 
Example 4
Source File: LinkMovementMethod2.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
@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) {
                ClickableSpan span = link[0];
                if (span instanceof URLSpan) {
                    UrlOpener.openUrl(widget.getContext(), ((URLSpan) span).getURL(), true);
                } else {
                    span.onClick(widget);
                }
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }

            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }

    return super.onTouchEvent(widget, buffer, event);
}
 
Example 5
Source File: TextDrawable.java    From litho with Apache License 2.0 4 votes vote down vote up
private boolean handleTouchForSpans(MotionEvent event, View view) {
  final int action = event.getActionMasked();
  if (action == ACTION_CANCEL) {
    clearSelection();
    resetLongClick();
    return false;
  }

  if (action == ACTION_MOVE && !mLongClickActivated && mLongClickRunnable != null) {
    trackLongClickBoundaryOnMove(event);
  }

  final boolean clickActivationAllowed = !mLongClickActivated;
  if (action == ACTION_UP) {
    resetLongClick();
  }

  final Rect bounds = getBounds();
  if (!isWithinBounds(bounds, event)) {
    return false;
  }

  final int x = (int) event.getX() - bounds.left;
  final int y = (int) event.getY() - bounds.top;

  ClickableSpan clickedSpan = getClickableSpanInCoords(x, y);

  if (clickedSpan == null && mClickableSpanExpandedOffset > 0) {
    clickedSpan = getClickableSpanInProximityToClick(x, y, mClickableSpanExpandedOffset);
  }

  if (clickedSpan == null) {
    clearSelection();
    return false;
  }

  if (action == ACTION_UP) {
    clearSelection();
    if (clickActivationAllowed
        && (mSpanListener == null || !mSpanListener.onClick(clickedSpan, view))) {
      clickedSpan.onClick(view);
    }
  } else if (action == ACTION_DOWN) {
    if (clickedSpan instanceof LongClickableSpan) {
      registerForLongClick((LongClickableSpan) clickedSpan, view);
    }
    setSelection(clickedSpan);
  }

  return true;
}
 
Example 6
Source File: LinkMovementMethod2.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
@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) {
                ClickableSpan span = link[0];
                if (span instanceof URLSpan) {
                    OpenUrlHelper.openUrl(mActivity, ((URLSpan) span).getURL(), true);
                } else {
                    span.onClick(widget);
                }
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }

            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }

    return super.onTouchEvent(widget, buffer, event);
}
 
Example 7
Source File: LinkMovementMethod2.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
@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) {
                ClickableSpan span = link[0];
                if (span instanceof URLSpan) {
                    UrlOpener.openUrl(widget.getContext(), ((URLSpan) span).getURL(), true);
                } else {
                    span.onClick(widget);
                }
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }

            return true;
        } else {
            Selection.removeSelection(buffer);
        }
    }

    return super.onTouchEvent(widget, buffer, event);
}