Example usage for android.text Layout getLineVisibleEnd

List of usage examples for android.text Layout getLineVisibleEnd

Introduction

In this page you can find the example usage for android.text Layout getLineVisibleEnd.

Prototype

public int getLineVisibleEnd(int line) 

Source Link

Document

Return the text offset after the last visible character (so whitespace is not counted) on the specified line.

Usage

From source file:com.facebook.litho.widget.TextSpec.java

@OnPopulateExtraAccessibilityNode
static void onPopulateExtraAccessibilityNode(AccessibilityNodeInfoCompat node, int extraNodeIndex,
        int componentBoundsLeft, int componentBoundsTop, @Prop(resType = STRING) CharSequence text,
        @FromBoundsDefined Layout textLayout, @FromBoundsDefined ClickableSpan[] clickableSpans) {
    final Spanned spanned = (Spanned) text;

    final ClickableSpan span = clickableSpans[extraNodeIndex];
    final int start = spanned.getSpanStart(span);
    final int end = spanned.getSpanEnd(span);
    final int startLine = textLayout.getLineForOffset(start);
    final int endLine = textLayout.getLineForOffset(end);

    // The bounds for multi-line strings should *only* include the first line.  This is because
    // Talkback triggers its click at the center point of these bounds, and if that center point
    // is outside the spannable, it will click on something else.  There is no harm in not outlining
    // the wrapped part of the string, as the text for the whole string will be read regardless of
    // the bounding box.
    final int selectionPathEnd = startLine == endLine ? end : textLayout.getLineVisibleEnd(startLine);

    textLayout.getSelectionPath(start, selectionPathEnd, sTempPath);
    sTempPath.computeBounds(sTempRectF, /* unused */true);

    sTempRect.set(componentBoundsLeft + (int) sTempRectF.left, componentBoundsTop + (int) sTempRectF.top,
            componentBoundsLeft + (int) sTempRectF.right, componentBoundsTop + (int) sTempRectF.bottom);

    if (sTempRect.isEmpty()) {
        // Text is not actually visible.
        // Override bounds so it doesn't crash ExploreByTouchHelper.java
        sTempRect.set(0, 0, 1, 1);//  w ww. ja  v  a  2  s  .com
        node.setBoundsInParent(sTempRect);
        node.setContentDescription(""); // make node non-focusable
        return;
    }

    node.setBoundsInParent(sTempRect);

    node.setClickable(true);
    node.setFocusable(true);
    node.setEnabled(true);
    node.setVisibleToUser(true);
    if (span instanceof AccessibleClickableSpan) {
        node.setText(((AccessibleClickableSpan) span).getAccessibilityDescription());
    } else {
        node.setText(spanned.subSequence(start, end));
    }
}