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

The following examples show how to use android.widget.TextView#getMeasuredWidth() . 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: EntryKeyboardView.java    From bither-android with Apache License 2.0 6 votes vote down vote up
@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
Source File: ViewUtils.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
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 File: SublimeTimePicker.java    From SublimePicker with 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
Source File: AppCompatTimePickerDelegate.java    From AppCompat-Extension-Library with 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 5
Source File: ScrollSlidingTextTabStrip.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
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
Source File: ScrollSlidingTextTabStrip.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@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 File: RxPopupViewCoordinatesFinder.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
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 9
Source File: ScrollSlidingTextTabStrip.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
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 10
Source File: ViewUtils.java    From Shield with 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 11
Source File: ViewUtils.java    From Shield with 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 12
Source File: Popup.java    From wallpaperboard with 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 13
Source File: SuggestionStripLayoutHelper.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
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 14
Source File: Popup.java    From candybar-library with 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
Source File: Popup.java    From candybar with 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 16
Source File: RxPopupViewCoordinatesFinder.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
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 17
Source File: ScrollSlidingTextTabStrip.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
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 18
Source File: LogsAdapter.java    From EdXposedManager with GNU General Public License v3.0 5 votes vote down vote up
@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 19
Source File: FilterTabsView.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
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 20
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);
}