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

The following examples show how to use android.widget.TextView#getMeasuredWidth() . These examples are extracted from open source projects. 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
@Override
public void getLocationInWindow(int[] location) {
    // override to make preview centered
    super.getLocationInWindow(location);
    try {
        TextView previewText = getPreviewText();
        float keyWidth = getKeyWidth();
        float keyGap = getKeyHorizontalGap();
        previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int previewWidth = previewText.getMeasuredWidth();
        if (previewWidth > keyWidth) {
            location[0] = (int) (location[0] + keyWidth / 2 - previewWidth / 2 + keyGap * 2);
        }
    } catch (Exception e) {
        e.printStackTrace();
        LogUtil.e(TAG, "can not center preview");
    }
}
 
Example 2
public static float GetLetterPositionX(@NonNull TextView aView, int aLetterIndex, boolean aClamp) {
    Layout layout = aView.getLayout();
    if (layout == null) {
        return 0;
    }
    float x = layout.getPrimaryHorizontal(aLetterIndex);
    x += aView.getPaddingLeft();
    x -= aView.getScrollX();
    if (aClamp && x > (aView.getMeasuredWidth() - aView.getPaddingRight())) {
        x = aView.getMeasuredWidth() - aView.getPaddingRight();
    }
    if (aClamp && x < aView.getPaddingLeft()) {
        x = aView.getPaddingLeft();
    }
    return x;
}
 
Example 3
Source Project: SublimePicker   File: SublimeTimePicker.java    License: Apache License 2.0 6 votes vote down vote up
private int computeStableWidth(TextView v, int maxNumber) {
    int maxWidth = 0;

    for (int i = 0; i < maxNumber; i++) {
        final String text = String.format("%02d", i);
        v.setText(text);
        v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

        final int width = v.getMeasuredWidth();
        if (width > maxWidth) {
            maxWidth = width;
        }
    }

    return maxWidth;
}
 
Example 4
private int computeStableWidth(TextView v, int maxNumber) {
    int maxWidth = 0;

    for (int i = 0; i < maxNumber; i++) {
        final String text = String.format("%02d", i);
        v.setText(text);
        v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

        final int width = v.getMeasuredWidth();
        if (width > maxWidth) {
            maxWidth = width;
        }
    }

    return maxWidth;
}
 
Example 5
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    if (prevLayoutWidth != r - l) {
        prevLayoutWidth = r - l;
        scrollingToChild = -1;
        if (animatingIndicator) {
            AndroidUtilities.cancelRunOnUIThread(animationRunnable);
            animatingIndicator = false;
            setEnabled(true);
            if (delegate != null) {
                delegate.onPageScrolled(1.0f);
            }
        }
        TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
        if (child != null) {
            indicatorWidth = getChildWidth(child);
            indicatorX = child.getLeft() + (child.getMeasuredWidth() - indicatorWidth) / 2;
        }
    }
}
 
Example 6
public static void setMdText(@NonNull TextView textView, String markdown) {
    if (!InputHelper.isEmpty(markdown)) {
        int width = textView.getMeasuredWidth();
        if (width > 0) {
            render(textView, markdown, width);
        } else {
            textView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override public boolean onPreDraw() {
                    textView.getViewTreeObserver().removeOnPreDrawListener(this);
                    render(textView, markdown, textView.getMeasuredWidth());
                    return true;
                }
            });
        }
    }
}
 
Example 7
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    if (prevLayoutWidth != r - l) {
        prevLayoutWidth = r - l;
        scrollingToChild = -1;
        if (animatingIndicator) {
            AndroidUtilities.cancelRunOnUIThread(animationRunnable);
            animatingIndicator = false;
            setEnabled(true);
            if (delegate != null) {
                delegate.onPageScrolled(1.0f);
            }
        }
        TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
        if (child != null) {
            indicatorWidth = getChildWidth(child);
            indicatorX = child.getLeft() + (child.getMeasuredWidth() - indicatorWidth) / 2;
        }
    }
}
 
Example 8
Source Project: candybar   File: Popup.java    License: Apache License 2.0 5 votes vote down vote up
private int getMeasuredWidth(@NonNull Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int maxWidth = context.getResources().getDimensionPixelSize(R.dimen.popup_max_width);
    int minWidth = context.getResources().getDimensionPixelSize(R.dimen.popup_min_width);
    String longestText = "";
    for (PopupItem item : mAdapter.getItems()) {
        if (item.getTitle().length() > longestText.length())
            longestText = item.getTitle();
    }

    int padding = context.getResources().getDimensionPixelSize(R.dimen.content_margin);
    int iconSize = context.getResources().getDimensionPixelSize(R.dimen.icon_size_small);
    TextView textView = new TextView(context);
    textView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    textView.setTypeface(TypefaceHelper.getRegular(context));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources()
            .getDimension(R.dimen.text_content_subtitle));
    textView.setPadding(padding + iconSize + padding, 0, padding, 0);
    textView.setText(longestText);

    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(metrics.widthPixels, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);

    int measuredWidth = textView.getMeasuredWidth() + padding;
    if (measuredWidth <= minWidth) {
        return minWidth;
    }

    if (measuredWidth >= minWidth && measuredWidth <= maxWidth) {
        return measuredWidth;
    }
    return maxWidth;
}
 
Example 9
private int getChildWidth(TextView child) {
    Layout layout = child.getLayout();
    if (layout != null) {
        int w = (int) Math.ceil(layout.getLineWidth(0)) + AndroidUtilities.dp(2);
        if (child.getCompoundDrawables()[2] != null) {
            w += child.getCompoundDrawables()[2].getIntrinsicWidth() + AndroidUtilities.dp(6);
        }
        return w;
    } else {
        return child.getMeasuredWidth();
    }
}
 
Example 10
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    TextView view = holder.textView;
    view.setText(logs.get(position));
    view.measure(0, 0);
    int desiredWidth = view.getMeasuredWidth();
    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
    layoutParams.width = desiredWidth;
    if (recyclerView.getWidth() < desiredWidth) {
        recyclerView.requestLayout();
    }
}
 
Example 11
public void selectTabWithId(int id, float progress) {
    int position = idToPosition.get(id, -1);
    if (position < 0) {
        return;
    }
    if (progress < 0) {
        progress = 0;
    } else if (progress > 1.0f) {
        progress = 1.0f;
    }
    TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
    TextView nextChild = (TextView) tabsContainer.getChildAt(position);
    if (child != null && nextChild != null) {
        animateIndicatorStartWidth = getChildWidth(child);
        animateIndicatorStartX = child.getLeft() + (child.getMeasuredWidth() - animateIndicatorStartWidth) / 2;
        animateIndicatorToWidth = getChildWidth(nextChild);
        animateIndicatorToX = nextChild.getLeft() + (nextChild.getMeasuredWidth() - animateIndicatorToWidth) / 2;
        setAnimationProgressInernal(nextChild, child, progress);
        if (progress >= 1f) {
            child.setTag(unactiveTextColorKey);
            nextChild.setTag(activeTextColorKey);
        }
        scrollToChild(tabsContainer.indexOfChild(nextChild));
    }
    if (progress >= 1.0f) {
        currentPosition = position;
        selectedTabId = id;
    }
}
 
Example 12
private static Point getPositionLeftTo(TextView tipView, RxPopupView rxPopupView, RxCoordinates anchorViewRxCoordinates, RxCoordinates rootLocation) {
    Point point = new Point();
    point.x = anchorViewRxCoordinates.left - tipView.getMeasuredWidth();
    AdjustLeftToOutOfBounds(tipView, rxPopupView.getRootView(), point, anchorViewRxCoordinates, rootLocation);
    point.y = anchorViewRxCoordinates.top + getYCenteringOffset(tipView, rxPopupView);
    return point;
}
 
Example 13
private static void AdjustRightToOutOfBounds(TextView tipView, ViewGroup root, Point point, RxCoordinates anchorViewRxCoordinates, RxCoordinates rootLocation) {
    ViewGroup.LayoutParams params = tipView.getLayoutParams();
    int availableSpace = rootLocation.right - root.getPaddingRight() - anchorViewRxCoordinates.right;
    if (point.x + tipView.getMeasuredWidth() > rootLocation.right - root.getPaddingRight()){
        params.width = availableSpace;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        tipView.setLayoutParams(params);
        measureViewWithFixedWidth(tipView, params.width);
    }
}
 
Example 14
Source Project: candybar-library   File: Popup.java    License: Apache License 2.0 5 votes vote down vote up
private int getMeasuredWidth(@NonNull Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int maxWidth = context.getResources().getDimensionPixelSize(R.dimen.popup_max_width);
    int minWidth = context.getResources().getDimensionPixelSize(R.dimen.popup_min_width);
    String longestText = "";
    for (PopupItem item : mAdapter.getItems()) {
        if (item.getTitle().length() > longestText.length())
            longestText = item.getTitle();
    }

    int padding = context.getResources().getDimensionPixelSize(R.dimen.content_margin);
    int iconSize = context.getResources().getDimensionPixelSize(R.dimen.icon_size_small);
    TextView textView = new TextView(context);
    textView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    textView.setTypeface(TypefaceHelper.getRegular(context));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources()
            .getDimension(R.dimen.text_content_subtitle));
    textView.setPadding(padding + iconSize + padding, 0, padding, 0);
    textView.setText(longestText);

    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(metrics.widthPixels, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);

    int measuredWidth = textView.getMeasuredWidth() + padding;
    if (measuredWidth <= minWidth) {
        return minWidth;
    }

    if (measuredWidth >= minWidth && measuredWidth <= maxWidth) {
        return measuredWidth;
    }
    return maxWidth;
}
 
Example 15
private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView,
        final int x) {
    final TextView debugInfoView = mDebugInfoViews.get(positionInStrip);
    final CharSequence debugInfo = debugInfoView.getText();
    if (debugInfo == null) {
        return;
    }
    placerView.addView(debugInfoView);
    debugInfoView.measure(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final int infoWidth = debugInfoView.getMeasuredWidth();
    final int y = debugInfoView.getMeasuredHeight();
    ViewLayoutUtils.placeViewAt(
            debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight());
}
 
Example 16
Source Project: wallpaperboard   File: Popup.java    License: Apache License 2.0 5 votes vote down vote up
private int getMeasuredWidth(@NonNull Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int maxWidth = context.getResources().getDimensionPixelSize(R.dimen.popup_max_width);
    int minWidth = context.getResources().getDimensionPixelSize(R.dimen.popup_min_width);
    String longestText = "";
    for (PopupItem item : mAdapter.getItems()) {
        if (item.getTitle().length() > longestText.length())
            longestText = item.getTitle();
    }

    int padding = context.getResources().getDimensionPixelSize(R.dimen.content_margin);
    int iconSize = context.getResources().getDimensionPixelSize(R.dimen.icon_size_small);
    TextView textView = new TextView(context);
    textView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    textView.setTypeface(TypefaceHelper.getRegular(context));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources()
            .getDimension(R.dimen.text_content_subtitle));
    textView.setPadding(padding + iconSize + padding, 0, padding, 0);
    textView.setText(longestText);

    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(metrics.widthPixels, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);

    int measuredWidth = textView.getMeasuredWidth() + padding;
    if (measuredWidth <= minWidth) {
        return minWidth;
    }

    if (measuredWidth >= minWidth && measuredWidth <= maxWidth) {
        return measuredWidth;
    }
    return maxWidth;
}
 
Example 17
Source Project: Shield   File: ViewUtils.java    License: MIT License 5 votes vote down vote up
public static int getTextViewWidth(TextView textView, String text, int textSize) {
    textView.setText(text);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return textView.getMeasuredWidth();
}
 
Example 18
Source Project: Shield   File: ViewUtils.java    License: MIT License 5 votes vote down vote up
public static int getTextViewWidth(TextView textView, String text) {
    textView.setText(text);
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return textView.getMeasuredWidth();
}
 
Example 19
private int getChildWidth(TextView child) {
    Layout layout = child.getLayout();
    if (layout != null) {
        return (int) Math.ceil(layout.getLineWidth(0)) + AndroidUtilities.dp(2);
    } else {
        return child.getMeasuredWidth();
    }
}
 
Example 20
@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);
}