Example usage for android.widget TextView getWidth

List of usage examples for android.widget TextView getWidth

Introduction

In this page you can find the example usage for android.widget TextView getWidth.

Prototype

@ViewDebug.ExportedProperty(category = "layout")
public final int getWidth() 

Source Link

Document

Return the width of your view.

Usage

From source file:Main.java

public static String getEllipsisedText(TextView textView) {
    try {//from   w w  w.  jav  a 2s.c  o m
        String text = textView.getText().toString();
        int lines = textView.getLineCount();
        int width = textView.getWidth();
        int len = text.length();

        Log.d("Test", "text-->" + text + "; lines-->" + lines + "; width-->" + width + ";len-->" + len);
        TextUtils.TruncateAt where = TextUtils.TruncateAt.END;
        TextPaint paint = textView.getPaint();

        StringBuffer result = new StringBuffer();

        int spos = 0, cnt, tmp, hasLines = 0;

        while (hasLines < lines - 1) {
            cnt = paint.breakText(text, spos, len, true, width, null);
            if (cnt >= len - spos) {
                result.append(text.substring(spos));
                break;
            }

            tmp = text.lastIndexOf('\n', spos + cnt - 1);

            if (tmp >= 0 && tmp < spos + cnt) {
                result.append(text.substring(spos, tmp + 1));
                spos += tmp + 1;
            } else {
                tmp = text.lastIndexOf(' ', spos + cnt - 1);
                if (tmp >= spos) {
                    result.append(text.substring(spos, tmp + 1));
                    spos += tmp + 1;
                } else {
                    result.append(text.substring(spos, cnt));
                    spos += cnt;
                }
            }

            hasLines++;
        }

        if (spos < len) {
            result.append(TextUtils.ellipsize(text.subSequence(spos, len), paint, (float) width, where));
        }

        return result.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.smssecure.smssecure.util.ViewUtil.java

public static CharSequence ellipsize(@Nullable CharSequence text, @NonNull TextView view) {
    if (TextUtils.isEmpty(text) || view.getWidth() == 0 || view.getEllipsize() != TruncateAt.END) {
        return text;
    } else {//from  w  w  w .j  a v  a 2s  .  co  m
        return TextUtils.ellipsize(text, view.getPaint(),
                view.getWidth() - view.getPaddingRight() - view.getPaddingLeft(), TruncateAt.END);
    }
}

From source file:Main.java

public static void animateTextViewMaxLinesChange(final TextView textView, final int maxLines,
        final int duration) {
    final int startHeight = textView.getMeasuredHeight();
    textView.setMaxLines(maxLines);/*from ww  w  .j a  v a2 s  .c o m*/
    textView.measure(View.MeasureSpec.makeMeasureSpec(textView.getWidth(), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    final int endHeight = textView.getMeasuredHeight();
    ObjectAnimator animation = ObjectAnimator.ofInt(textView, MAX_HEIGHT_ATTR, startHeight, endHeight);
    animation.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            if (textView.getMaxHeight() == endHeight) {
                textView.setMaxLines(maxLines);
            }
        }
    }

    );
    animation.setDuration(duration).start();
}

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;/*from  w  w  w. j  ava  2s. com*/

    switch (gravity) {
    case Gravity.END:
        ratio = -1;
    case Gravity.START:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics = view.getPaint().getFontMetrics();
        total = fontMetrics.descent - fontMetrics.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;//from  w  w  w  .  j  a va2 s  . c  o m

    switch (gravity) {
    case Gravity.RIGHT:
        ratio = -1;
    case Gravity.LEFT:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics0 = view.getPaint().getFontMetrics();
        total = fontMetrics0.descent - fontMetrics0.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:com.frostwire.android.gui.views.KeywordTagView.java

private void updateComponents() {
    SpannableStringBuilder sb = new SpannableStringBuilder();
    sb = append(sb, keywordFilter.getKeyword(), keywordSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    if (count != -1) {
        sb = append(sb, "  (" + String.valueOf(count) + ")", countSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }//w w  w.  ja v  a2  s  .  c  om
    setText(sb, TextView.BufferType.NORMAL);

    if (dismissible) {
        setBackgroundResource(R.drawable.keyword_tag_background_active);
        int drawableResId = keywordFilter.isInclusive() ? R.drawable.keyword_tag_filter_add
                : R.drawable.keyword_tag_filter_minus;
        setCompoundDrawablesWithIntrinsicBounds(drawableResId, 0,
                R.drawable.keyword_tag_close_clear_cancel_full, 0);
        setTextColor(ContextCompat.getColor(getContext(), R.color.app_text_white));
    } else {
        setBackgroundResource(R.drawable.keyword_tag_background);
        setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        setTextColor(ContextCompat.getColor(getContext(), R.color.app_text_primary));
    }

    setOnClickListener(v -> onTouched());
    if (dismissible) {
        setOnTouchListener((v, event) -> {
            if (event.getAction() != MotionEvent.ACTION_UP) {
                return false;
            }
            TextView tv = (TextView) v;
            if (event.getX() >= tv.getWidth() - tv.getTotalPaddingRight()) {
                onDismissed();
                return true;
            }
            return false;
        });
    }
}

From source file:com.viewpagerindicator.TabMovablePageIndicator.java

private void moveIndicator(TextView tabView) {
    int left = tabView.getLeft();
    int width = tabView.getWidth();
    int textWidth = getTextWidth(tabView.getText().toString(), tabView.getTextSize(), tabView.getTypeface());

    int x = left + (width - textWidth) / 2;

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(textWidth,
            mIndicator.getLayoutParams().height);
    lp.leftMargin = x;/*from  w  w w.j  a  v  a2 s.  c  o m*/
    mIndicator.setLayoutParams(lp);
}

From source file:dcheungaa.procal.MainActivity.java

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    TextView cursor = (TextView) MainActivity.views.get("cursor");
    TextView matrixDisplay = (TextView) MainActivity.views.get("matrixDisplay");
    super.onWindowFocusChanged(hasFocus);
    if (call_load) {
        call_load = false;/*from w  w  w.j  a  va 2  s. c o  m*/
        fontWidth = cursor.getWidth();
        fontHeight = cursor.getHeight();
        cursor.setText(Character.toString((char) 0x258E));
        //cursor.setTop(matrixDisplay.getTop());
        cursor.setPadding(matrixDisplay.getPaddingLeft(), cursor.getPaddingTop(), cursor.getPaddingRight(),
                cursor.getPaddingBottom());
        cursor.setLeft(matrixDisplay.getLeft());
        //CursorHandler.hideCursor();
        RelativeLayout cm = (RelativeLayout) findViewById(R.id.content_main);
        LinearLayout rows = (LinearLayout) findViewById(R.id.llKeyPad);
        keyPad.KeyPad_resize(cm, rows);
        int fnBtnHeight = keyPad.btn_rows.get(0).get(0).get_mheight();
        System.out.print(Integer.toString(fnBtnHeight));
        varPad.resize(fnBtnHeight, fnBtnHeight * 3, svVar);
        cmdPad.resize(fnBtnHeight, fnBtnHeight * 3, svCmd);
        constPad.resize(fnBtnHeight, fnBtnHeight * 3, svConst);
    }
}

From source file:com.android.inputmethod.latin.suggestions.SuggestionStripLayoutHelper.java

public void layoutImportantNotice(final View importantNoticeStrip, final String importantNoticeTitle) {
    final TextView titleView = (TextView) importantNoticeStrip.findViewById(R.id.important_notice_title);
    final int width = titleView.getWidth() - titleView.getPaddingLeft() - titleView.getPaddingRight();
    titleView.setTextColor(mColorAutoCorrect);
    titleView.setText(importantNoticeTitle);
    titleView.setTextScaleX(1.0f); // Reset textScaleX.
    final float titleScaleX = getTextScaleX(importantNoticeTitle, width, titleView.getPaint());
    titleView.setTextScaleX(titleScaleX);
}

From source file:fr.jerome.climbinggymlog.view.googletools.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//from w w  w  . ja  v  a  2  s.com
 */
protected TextView createDefaultTabView(Context context) {
    final TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.post(new Runnable() {
        @Override
        public void run() {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
            if (textView.getWidth() < ((View) textView.getParent().getParent()).getWidth() * 0.33f) {
                params.width = 0;
                params.weight = 0.33f;
            }
            textView.requestLayout();
        }
    });
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}