Java Code Examples for android.text.Layout#getLineWidth()

The following examples show how to use android.text.Layout#getLineWidth() . 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: BetterLinkMovementExtended.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
private BetterLinkMovementExtended.ClickableSpanWithText findClickableSpanUnderTouch(TextView textView, Spannable text, MotionEvent event) {
    int touchX = (int) event.getX();
    int touchY = (int) event.getY();
    touchX -= textView.getTotalPaddingLeft();
    touchY -= textView.getTotalPaddingTop();
    touchX += textView.getScrollX();
    touchY += textView.getScrollY();
    Layout layout = textView.getLayout();
    int touchedLine = layout.getLineForVertical(touchY);
    int touchOffset = layout.getOffsetForHorizontal(touchedLine, (float) touchX);
    this.touchedLineBounds.left = layout.getLineLeft(touchedLine);
    this.touchedLineBounds.top = (float) layout.getLineTop(touchedLine);
    this.touchedLineBounds.right = layout.getLineWidth(touchedLine) + this.touchedLineBounds.left;
    this.touchedLineBounds.bottom = (float) layout.getLineBottom(touchedLine);
    if (this.touchedLineBounds.contains((float) touchX, (float) touchY)) {
        Object[] spans = text.getSpans(touchOffset, touchOffset, SPAN_CLASS);
        for (Object span : spans) {
            if (span instanceof ClickableSpan) {
                return ClickableSpanWithText.ofSpan(textView, (ClickableSpan) span);
            }
        }
        return null;
    } else {
        return null;
    }
}
 
Example 2
Source File: GDPRViewManager.java    From GDPRDialog with Apache License 2.0 5 votes vote down vote up
private float getMaxLineWidth(TextView textView) {
    Layout layout = textView.getLayout();
    float max_width = 0.0f;
    int lines = layout.getLineCount();
    for (int i = 0; i < lines; i++) {
        if (layout.getLineWidth(i) > max_width) {
            max_width = layout.getLineWidth(i);
        }
    }
    return max_width;
}
 
Example 3
Source File: WrapWidthTextView.java    From show-case-card-view with Apache License 2.0 5 votes vote down vote up
private float getMaxLineWidth(Layout layout) {
    float max_width = 0.0f;
    int lines = layout.getLineCount();
    for (int i = 0; i < lines; i++) {
        if (layout.getLineWidth(i) > max_width) {
            max_width = layout.getLineWidth(i);
        }
    }
    return max_width;
}
 
Example 4
Source File: AlignTextView.java    From AndroidUiKit with Apache License 2.0 5 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (needCheckDevice && isVivoApi22()) {

        int width = getMeasuredWidth();
        CharSequence charSequence = getText();
        Layout layout = createWorkingLayout(charSequence, getPaint(), width);
        float dotWidth = getPaint().measureText(DOT_SUFFIX);
        int lineCount = layout.getLineCount();
        Log.d(TAG, "onMeasure , lineCount : " + lineCount);
        // 多行且第一行未占满
        if (lineCount > 1 && Math.abs(width - layout.getLineWidth(0)) > dotWidth) {
            Log.d(TAG, "onMeasure , firstLineEndIndex : " + layout.getLineEnd(0));
            int index = layout.getLineEnd(1);
            while (true) {
                index--;
                charSequence = charSequence.subSequence(0, index);
                charSequence = SpannableStringBuilder.valueOf(charSequence).append(DOT_SUFFIX);
                layout = createWorkingLayout(charSequence, getPaint(), width);
                if (layout.getLineCount() == 1 && (width - layout.getLineWidth(0) <= dotWidth || index <= 10)) {
                    mLayout = layout;
                    break;
                }
            }
        }
        needCheckDevice = false;
    }
}
 
Example 5
Source File: MultiLineWrapContentWidthTextView.java    From linphone-android with GNU General Public License v3.0 5 votes vote down vote up
private float getMaxLineWidth(Layout layout) {
    float max_width = 0.0f;
    int lines = layout.getLineCount();
    for (int i = 0; i < lines; i++) {
        if (layout.getLineWidth(i) > max_width) {
            max_width = layout.getLineWidth(i);
        }
    }
    return max_width;
}
 
Example 6
Source File: TabLayout.java    From a with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Approximates a given lines width with the new provided text size.
 */
private float approximateLineWidth(Layout layout, int line, float textSize) {
    return layout.getLineWidth(line) * (textSize / layout.getPaint().getTextSize());
}
 
Example 7
Source File: TabLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
/** Approximates a given lines width with the new provided text size. */
private float approximateLineWidth(@NonNull Layout layout, int line, float textSize) {
  return layout.getLineWidth(line) * (textSize / layout.getPaint().getTextSize());
}
 
Example 8
Source File: BubbleTextContainer.java    From actor-platform with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Rect bounds = new Rect();
    Drawable background = getBackground();
    if (background != null) {
        background.getPadding(bounds);
    }

    int wMode = MeasureSpec.getMode(widthMeasureSpec);
    int maxW = MeasureSpec.getSize(widthMeasureSpec) - bounds.left - bounds.right;

    TextView messageView = (TextView) getChildAt(0);
    messageView.measure(MeasureSpec.makeMeasureSpec(maxW, wMode), heightMeasureSpec);
    View timeView = getChildAt(1);
    timeView.measure(MeasureSpec.makeMeasureSpec(maxW, wMode), heightMeasureSpec);

    Layout textLayout = messageView.getLayout();

    int contentW = messageView.getMeasuredWidth();
    int timeW = timeView.getMeasuredWidth();
    boolean isRtl = BidiFormatter.getInstance().isRtl(messageView.getText().toString());

    if (messageView.getLayout().getLineCount() < 5 && !isRtl) {
        contentW = 0;
        for (int i = 0; i < textLayout.getLineCount(); i++) {
            contentW = Math.max(contentW, (int) textLayout.getLineWidth(i));
        }
    }

    int lastLineW = (int) textLayout.getLineWidth(textLayout.getLineCount() - 1);

    if (isRtl) {
        lastLineW = contentW;
    }

    int fullContentW, fullContentH;

    if (isRtl) {
        fullContentW = contentW;
        fullContentH = messageView.getMeasuredHeight() + timeView.getMeasuredHeight();
    } else {
        if (lastLineW + timeW < contentW) {
            // Nothing to do
            fullContentW = contentW;
            fullContentH = messageView.getMeasuredHeight();
        } else if (lastLineW + timeW < maxW) {
            fullContentW = lastLineW + timeW;
            fullContentH = messageView.getMeasuredHeight();
        } else {
            fullContentW = contentW;
            fullContentH = messageView.getMeasuredHeight() + timeView.getMeasuredHeight();
        }
    }

    setMeasuredDimension(fullContentW + bounds.left + bounds.right, fullContentH + bounds.top + bounds.bottom);
}