Java Code Examples for android.widget.TextView#scrollTo()

The following examples show how to use android.widget.TextView#scrollTo() . 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: ScrollingMovementMethod.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void onTakeFocus(TextView widget, Spannable text, int dir) {
    Layout layout = widget.getLayout();

    if (layout != null && (dir & View.FOCUS_FORWARD) != 0) {
        widget.scrollTo(widget.getScrollX(),
                        layout.getLineTop(0));
    }
    if (layout != null && (dir & View.FOCUS_BACKWARD) != 0) {
        int padding = widget.getTotalPaddingTop() +
                      widget.getTotalPaddingBottom();
        int line = layout.getLineCount() - 1;

        widget.scrollTo(widget.getScrollX(),
                        layout.getLineTop(line+1) -
                        (widget.getHeight() - padding));
    }
}
 
Example 2
Source File: BaseMovementMethod.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a scroll left action.
 * Scrolls left by the specified number of characters.
 *
 * @param widget The text view.
 * @param buffer The text buffer.
 * @param amount The number of characters to scroll by.  Must be at least 1.
 * @return True if the event was handled.
 * @hide
 */
protected boolean scrollLeft(TextView widget, Spannable buffer, int amount) {
    final int minScrollX = getScrollBoundsLeft(widget);
    int scrollX = widget.getScrollX();
    if (scrollX > minScrollX) {
        scrollX = Math.max(scrollX - getCharacterWidth(widget) * amount, minScrollX);
        widget.scrollTo(scrollX, widget.getScrollY());
        return true;
    }
    return false;
}
 
Example 3
Source File: BaseMovementMethod.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a scroll right action.
 * Scrolls right by the specified number of characters.
 *
 * @param widget The text view.
 * @param buffer The text buffer.
 * @param amount The number of characters to scroll by.  Must be at least 1.
 * @return True if the event was handled.
 * @hide
 */
protected boolean scrollRight(TextView widget, Spannable buffer, int amount) {
    final int maxScrollX = getScrollBoundsRight(widget) - getInnerWidth(widget);
    int scrollX = widget.getScrollX();
    if (scrollX < maxScrollX) {
        scrollX = Math.min(scrollX + getCharacterWidth(widget) * amount, maxScrollX);
        widget.scrollTo(scrollX, widget.getScrollY());
        return true;
    }
    return false;
}
 
Example 4
Source File: BaseMovementMethod.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a scroll to line start action.
 * Scrolls to the start of the line.
 *
 * @param widget The text view.
 * @param buffer The text buffer.
 * @return True if the event was handled.
 * @hide
 */
protected boolean scrollLineStart(TextView widget, Spannable buffer) {
    final int minScrollX = getScrollBoundsLeft(widget);
    int scrollX = widget.getScrollX();
    if (scrollX > minScrollX) {
        widget.scrollTo(minScrollX, widget.getScrollY());
        return true;
    }
    return false;
}
 
Example 5
Source File: BaseMovementMethod.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a scroll to line end action.
 * Scrolls to the end of the line.
 *
 * @param widget The text view.
 * @param buffer The text buffer.
 * @return True if the event was handled.
 * @hide
 */
protected boolean scrollLineEnd(TextView widget, Spannable buffer) {
    final int maxScrollX = getScrollBoundsRight(widget) - getInnerWidth(widget);
    int scrollX = widget.getScrollX();
    if (scrollX < maxScrollX) {
        widget.scrollTo(maxScrollX, widget.getScrollY());
        return true;
    }
    return false;
}
 
Example 6
Source File: CharacteristicOperationFragment.java    From FastBle with Apache License 2.0 5 votes vote down vote up
private void addText(TextView textView, String content) {
    textView.append(content);
    textView.append("\n");
    int offset = textView.getLineCount() * textView.getLineHeight();
    if (offset > textView.getHeight()) {
        textView.scrollTo(0, offset - textView.getHeight());
    }
}
 
Example 7
Source File: Touch.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Scrolls the specified widget to the specified coordinates, except
 * constrains the X scrolling position to the horizontal regions of
 * the text that will be visible after scrolling to the specified
 * Y position.
 */
public static void scrollTo(TextView widget, Layout layout, int x, int y) {
    final int horizontalPadding = widget.getTotalPaddingLeft() + widget.getTotalPaddingRight();
    final int availableWidth = widget.getWidth() - horizontalPadding;

    final int top = layout.getLineForVertical(y);
    Alignment a = layout.getParagraphAlignment(top);
    boolean ltr = layout.getParagraphDirection(top) > 0;

    int left, right;
    if (widget.getHorizontallyScrolling()) {
        final int verticalPadding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
        final int bottom = layout.getLineForVertical(y + widget.getHeight() - verticalPadding);

        left = Integer.MAX_VALUE;
        right = 0;

        for (int i = top; i <= bottom; i++) {
            left = (int) Math.min(left, layout.getLineLeft(i));
            right = (int) Math.max(right, layout.getLineRight(i));
        }
    } else {
        left = 0;
        right = availableWidth;
    }

    final int actualWidth = right - left;

    if (actualWidth < availableWidth) {
        if (a == Alignment.ALIGN_CENTER) {
            x = left - ((availableWidth - actualWidth) / 2);
        } else if ((ltr && (a == Alignment.ALIGN_OPPOSITE)) ||
                   (!ltr && (a == Alignment.ALIGN_NORMAL)) ||
                   (a == Alignment.ALIGN_RIGHT)) {
            // align_opposite does NOT mean align_right, we need the paragraph
            // direction to resolve it to left or right
            x = left - (availableWidth - actualWidth);
        } else {
            x = left;
        }
    } else {
        x = Math.min(x, right - availableWidth);
        x = Math.max(x, left);
    }

    widget.scrollTo(x, y);
}