Example usage for android.text Layout getLineForOffset

List of usage examples for android.text Layout getLineForOffset

Introduction

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

Prototype

public int getLineForOffset(int offset) 

Source Link

Document

Get the line number on which the specified text offset appears.

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);//from w  ww. ja v a2s  .  co  m
        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));
    }
}

From source file:com.amazon.android.ui.widget.EllipsizedTextView.java

/**
 * This function accomplishes two goals//from w  w w  . j  a  va2 s .  c o  m
 * <p>
 * # Measure all text using a staticLayout to determine where the text should be ellipsized
 * # Moves the ellipsis back one word to create more space for the image
 * # Replaces the ellipsis with a guillemet image that changes on focus
 * # Measures/Renders only the text visible
 * <p>
 * This function is slow and should only be called when needed
 * <p>
 * There is a bug in android that doesn't allow ellipsis to appear in multiline text fields
 * when the text is specified by Spannable.
 * <p>
 * To get around this we:
 * # Force this to use just text
 * # Layout twice once with just text, the second time with a Spannable with an image for an
 * ellipse
 */
private void reflow(final int widthMeasureSpec, final int heightMeasureSpec) {
    // First layout
    super.setText(mCharSequence, BufferType.SPANNABLE);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    final Layout layout = getLayout();
    if (layout == null) {
        // This will occur if the text is empty
        return;
    }

    final int lastLine = layout.getLineForOffset(mSetText.length());
    final int maxLines = getMaxLines();

    // Check if text should have an ellipsis.
    if (lastLine >= maxLines) {
        setEllipsis(widthMeasureSpec, heightMeasureSpec, layout, lastLine, maxLines);
    } else {
        // Reapply spans.
        if (mCharSequence instanceof Spannable) {
            final Spannable span = (Spannable) mCharSequence;
            super.setText(span, BufferType.SPANNABLE);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
        // No ellipsis, make this not focusable.
        removeEllipsisProperties();
    }
}

From source file:nl.mpcjanssen.simpletask.AddTask.java

public int getCurrentCursorLine(@NotNull EditText editText) {
    int selectionStart = Selection.getSelectionStart(editText.getText());
    Layout layout = editText.getLayout();

    if (selectionStart != -1) {
        return layout.getLineForOffset(selectionStart);
    }//  ww  w  .  j  a  v  a  2s. com

    return -1;
}

From source file:com.apptentive.android.sdk.module.messagecenter.view.MessageCenterActivityContent.java

@Override
public void OnListViewResize(int w, int h, int oldw, int oldh) {
    // detect keyboard launching
    if (oldh > h) {
        if (composingItem != null) {
            // When keyboard is up, adjust the scolling such that the cursor is always visible
            final int firstIndex = messageCenterListView.getFirstVisiblePosition();
            int lastIndex = messageCenterListView.getLastVisiblePosition();
            View v = messageCenterListView.getChildAt(lastIndex - firstIndex);
            int top = (v == null) ? 0 : v.getTop();
            if (messageEditText != null) {
                int pos = messageEditText.getSelectionStart();
                Layout layout = messageEditText.getLayout();
                int line = layout.getLineForOffset(pos);
                int baseline = layout.getLineBaseline(line);
                int ascent = layout.getLineAscent(line);
                messageCenterViewHandler.sendMessage(messageCenterViewHandler.obtainMessage(MSG_SCROLL_FROM_TOP,
                        lastIndex, Math.max(top - (oldh - h), baseline - ascent)));
            }/*from  w w  w .  jav a2s  .  c o  m*/
        }
    }
}

From source file:edu.mit.viral.shen.DroidFish.java

@Override
public void moveListUpdated() {
    // moveList.setText("hi");
    // moveList.setText(gameTextListener.getSpannableData());
    Layout layout = moveList.getLayout();
    if (layout != null) {
        int currPos = gameTextListener.getCurrPos();
        int line = layout.getLineForOffset(currPos);
        int y = (int) ((line - 1.5) * moveList.getLineHeight());
        moveListScroll.scrollTo(0, y);/*from  w w  w. java 2 s. co  m*/
    }
}

From source file:com.nttec.everychan.ui.presentation.BoardFragment.java

private Point getSpanCoordinates(View widget, ClickableURLSpan span) {
    TextView parentTextView = (TextView) widget;

    Rect parentTextViewRect = new Rect();

    // Initialize values for the computing of clickedText position
    SpannableString completeText = (SpannableString) (parentTextView).getText();
    Layout textViewLayout = parentTextView.getLayout();

    int startOffsetOfClickedText = completeText.getSpanStart(span);
    int endOffsetOfClickedText = completeText.getSpanEnd(span);
    double startXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal(startOffsetOfClickedText);
    double endXCoordinatesOfClickedText = textViewLayout.getPrimaryHorizontal(endOffsetOfClickedText);

    // Get the rectangle of the clicked text
    int currentLineStartOffset = textViewLayout.getLineForOffset(startOffsetOfClickedText);
    int currentLineEndOffset = textViewLayout.getLineForOffset(endOffsetOfClickedText);
    boolean keywordIsInMultiLine = currentLineStartOffset != currentLineEndOffset;
    textViewLayout.getLineBounds(currentLineStartOffset, parentTextViewRect);

    // Update the rectangle position to his real position on screen
    int[] parentTextViewLocation = { 0, 0 };
    parentTextView.getLocationOnScreen(parentTextViewLocation);

    double parentTextViewTopAndBottomOffset = (parentTextViewLocation[1] - parentTextView.getScrollY()
            + parentTextView.getCompoundPaddingTop());

    Rect windowRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowRect);
    parentTextViewTopAndBottomOffset -= windowRect.top;

    parentTextViewRect.top += parentTextViewTopAndBottomOffset;
    parentTextViewRect.bottom += parentTextViewTopAndBottomOffset;

    parentTextViewRect.left += (parentTextViewLocation[0] + startXCoordinatesOfClickedText
            + parentTextView.getCompoundPaddingLeft() - parentTextView.getScrollX());
    parentTextViewRect.right = (int) (parentTextViewRect.left + endXCoordinatesOfClickedText
            - startXCoordinatesOfClickedText);

    int x = (parentTextViewRect.left + parentTextViewRect.right) / 2;
    int y = (parentTextViewRect.top + parentTextViewRect.bottom) / 2;
    if (keywordIsInMultiLine) {
        x = parentTextViewRect.left;/*from   ww  w .j a  v a2  s .  co  m*/
    }

    return new Point(x, y);
}

From source file:com.if3games.chessonline.DroidFish.java

@Override
public void moveListUpdated() {
    moveList.setText(gameTextListener.getSpannableData());
    Layout layout = moveList.getLayout();
    if (layout != null) {
        int currPos = gameTextListener.getCurrPos();
        int line = layout.getLineForOffset(currPos);
        int y = (int) ((line - 1.5) * moveList.getLineHeight());
        moveListScroll.scrollTo(0, y);/*w  ww  .  j  a  va2  s  .co m*/
    }
}