Example usage for android.widget TextView getMeasuredWidth

List of usage examples for android.widget TextView getMeasuredWidth

Introduction

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

Prototype

public final int getMeasuredWidth() 

Source Link

Document

Like #getMeasuredWidthAndState() , but only returns the raw width component (that is the result is masked by #MEASURED_SIZE_MASK ).

Usage

From source file:Main.java

public static int getTextWidth(TextView textView) {
    return textView.getMeasuredWidth() - textView.getCompoundPaddingLeft() - textView.getCompoundPaddingRight();
}

From source file:Main.java

protected static Bitmap creatCodeBitmap(String contents, int width, int height, Context context) {
    TextView tv = new TextView(context);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(layoutParams);//w w w.  j a va 2s .  co m
    tv.setText(contents);
    tv.setHeight(height);
    tv.setGravity(Gravity.CENTER_HORIZONTAL);
    tv.setWidth(width);
    tv.setDrawingCacheEnabled(true);
    tv.setTextColor(Color.BLACK);
    tv.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());

    tv.buildDrawingCache();
    Bitmap bitmapCode = tv.getDrawingCache();
    return bitmapCode;
}

From source file:com.fastbootmobile.encore.utils.Utils.java

/**
 * Calculates the optimal size of the text based on the text view width
 *
 * @param textView     The text view in which the text should fit
 * @param desiredWidth The desired final text width, or -1 for the TextView's getMeasuredWidth
 *///from w  w w.  j  a  v  a  2 s .c o  m
public static void adjustTextSize(TextView textView, int desiredWidth) {
    if (desiredWidth <= 0) {
        desiredWidth = textView.getMeasuredWidth();

        if (desiredWidth <= 0) {
            // Invalid width, don't do anything
            Log.w("Utils", "adjustTextSize: Not doing anything (measured width invalid)");
            return;
        }
    }

    // Add some margin to width
    desiredWidth *= 0.8f;

    Paint paint = new Paint();
    Rect bounds = new Rect();

    paint.setTypeface(textView.getTypeface());
    float textSize = textView.getTextSize() * 2.0f;
    paint.setTextSize(textSize);
    String text = textView.getText().toString();
    paint.getTextBounds(text, 0, text.length(), bounds);

    while (bounds.width() > desiredWidth) {
        textSize--;
        paint.setTextSize(textSize);
        paint.getTextBounds(text, 0, text.length(), bounds);
    }

    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}

From source file:ecust.news.myWidgetTabPageIndicator.java

public void setCurrentItem(int position) {
    //// w w w .  j av a 2 s . co m
    for (int i = 0; i < linearLayout.getChildCount(); i++) {
        TextView textView = (TextView) linearLayout.getChildAt(i);
        if (i != position) {
            textView.setTextColor(textViewUnfocusedColor);
            textView.setCompoundDrawables(null, null, null, null);
        } else {
            textView.setTextColor(textViewFocusedColor);

            //drawable
            textView.measure(0, 0);
            int w = textView.getMeasuredWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
            int h = Global.dimenConvert.dip2px(dp_FocusedLineHeight);
            Drawable drawable = new ColorDrawable(Color.WHITE);
            drawable.setBounds(0, 0, w, h);
            textView.setCompoundDrawables(null, null, null, drawable);
        }

    }

    //?textView??
    int sum_width = 0;
    for (int i = 0; i < position; i++) {
        sum_width += linearLayout.getChildAt(i).getWidth();
    }

    //?
    sum_width -= this.getWidth() / 2 - linearLayout.getChildAt(position).getWidth() / 2;

    //
    this.smoothScrollTo(sum_width, 0);
}

From source file:angeloid.dreamnarae.SwipeyTabs.java

/**
 * Calculate the position of the tabs./*from  w  w  w  .j a  v  a 2s .c o m*/
 * 
 * @param position
 *            the position of the fronted tab
 * @param tabPositions
 *            the array in which to store the result
 */
private void calculateTabPosition(int position, int[] tabPositions) {
    if (mAdapter == null) {
        return;
    }

    final int count = mAdapter.getCount();

    if (position >= 0 && position < count) {
        final int width = getMeasuredWidth();

        final View centerTab = getChildAt(position);
        tabPositions[position] = width / 2 - centerTab.getMeasuredWidth() / 2;
        for (int i = position - 1; i >= 0; i--) {
            final TextView tab = (TextView) getChildAt(i);
            if (i == position - 1) {
                tabPositions[i] = 0 - tab.getPaddingLeft();
            } else {
                tabPositions[i] = 0 - tab.getMeasuredWidth() - width;
            }
            tabPositions[i] = Math.min(tabPositions[i], tabPositions[i + 1] - tab.getMeasuredWidth());
        }
        for (int i = position + 1; i < count; i++) {
            final TextView tab = (TextView) getChildAt(i);
            if (i == position + 1) {
                tabPositions[i] = width - tab.getMeasuredWidth() + tab.getPaddingRight();
            } else {
                tabPositions[i] = width * 2;
            }
            final TextView prevTab = (TextView) getChildAt(i - 1);
            tabPositions[i] = Math.max(tabPositions[i], tabPositions[i - 1] + prevTab.getMeasuredWidth());
        }
    } else {
        for (int i = 0; i < tabPositions.length; i++) {
            tabPositions[i] = -1;
        }
    }
}

From source file:com.ezmeal.swipeytabs.SwipeyTabs.java

/**
 * Calculate the position of the tabs.//from   w w w  . j  av a 2  s . c  o m
 * 
 * @param position
 *            the position of the fronted tab
 * @param tabPositions
 *            the array in which to store the result
 */
private void calculateTabPosition(int position, int[] tabPositions) {
    if (mAdapter == null) {
        return;
    }

    final int count = mAdapter.getCount();

    if (position >= 0 && position < count) {
        final int width = getMeasuredWidth();

        final View centerTab = getChildAt(position);
        tabPositions[position] = width / 2 - centerTab.getMeasuredWidth() / 2;
        for (int i = position - 1; i >= 0; i--) {
            final TextView tab = (TextView) getChildAt(i);
            if (i == position - 1) {
                tabPositions[i] = 5 - tab.getPaddingLeft();
            } else if (i != 0 && i != count - 1) {
                tabPositions[i] = 0 - tab.getMeasuredWidth() - width;
            }
            tabPositions[i] = Math.min(tabPositions[i], tabPositions[i + 1] - tab.getMeasuredWidth());
        }

        for (int i = position + 1; i < count; i++) {
            final TextView tab = (TextView) getChildAt(i);
            if (i == position + 1) {
                tabPositions[i] = width - tab.getMeasuredWidth() + tab.getPaddingRight() - 5;
            } else if (i != 0 && i != count - 1) {
                tabPositions[i] = width * 2;
            }
            final TextView prevTab = (TextView) getChildAt(i - 1);
            tabPositions[i] = Math.max(tabPositions[i], tabPositions[i - 1] + prevTab.getMeasuredWidth());
        }

        /*
        // make it a circle!
        if (position == 0) {
           final TextView tab = (TextView) getChildAt(count - 1);
           tabPositions[count - 1] = 5 - tab.getPaddingLeft();
        }
        if (position == count - 1) {
           final TextView tab = (TextView) getChildAt(0);
           tabPositions[0] = width - tab.getMeasuredWidth()
          + tab.getPaddingRight() - 5;
        }
        */

    } else {
        for (int i = 0; i < tabPositions.length; i++) {
            tabPositions[i] = -1;
        }

    }
}

From source file:com.github.shareme.gwsmaterialuikit.library.material.widget.TabPageIndicator.java

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    TextView tv = getTabView(mSelectedPosition);
    if (tv != null)
        updateIndicator(tv.getLeft(), tv.getMeasuredWidth());
}

From source file:com.github.shareme.gwsmaterialuikit.library.material.widget.TabPageIndicator.java

@Override
public void onPageScrollStateChanged(int state) {
    if (state == ViewPager.SCROLL_STATE_IDLE) {
        mScrolling = false;// ww  w  . j  a v  a  2s. c o m
        TextView tv = getTabView(mSelectedPosition);
        if (tv != null) {
            updateIndicator(tv.getLeft(), tv.getMeasuredWidth());
        }
    } else
        mScrolling = true;

    if (mListener != null)
        mListener.onPageScrollStateChanged(state);
}

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

/**
 * Layout suggestions to the suggestions strip. And returns the start index of more
 * suggestions./*from w w w .j a v a  2 s  .  com*/
 *
 * @param suggestedWords suggestions to be shown in the suggestions strip.
 * @param stripView the suggestions strip view.
 * @param placerView the view where the debug info will be placed.
 * @return the start index of more suggestions.
 */
public int layoutAndReturnStartIndexOfMoreSuggestions(final SuggestedWords suggestedWords,
        final ViewGroup stripView, final ViewGroup placerView) {
    if (suggestedWords.isPunctuationSuggestions()) {
        return layoutPunctuationsAndReturnStartIndexOfMoreSuggestions((PunctuationSuggestions) suggestedWords,
                stripView);
    }

    final int startIndexOfMoreSuggestions = setupWordViewsAndReturnStartIndexOfMoreSuggestions(suggestedWords,
            mSuggestionsCountInStrip);
    final TextView centerWordView = mWordViews.get(mCenterPositionInStrip);
    final int stripWidth = stripView.getWidth();
    final int centerWidth = getSuggestionWidth(mCenterPositionInStrip, stripWidth);
    if (suggestedWords.size() == 1 || getTextScaleX(centerWordView.getText(), centerWidth,
            centerWordView.getPaint()) < MIN_TEXT_XSCALE) {
        // Layout only the most relevant suggested word at the center of the suggestion strip
        // by consolidating all slots in the strip.
        final int countInStrip = 1;
        mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip);
        layoutWord(mCenterPositionInStrip, stripWidth - mPadding);
        stripView.addView(centerWordView);
        setLayoutWeight(centerWordView, 1.0f, ViewGroup.LayoutParams.MATCH_PARENT);
        if (SuggestionStripView.DBG) {
            layoutDebugInfo(mCenterPositionInStrip, placerView, stripWidth);
        }
        final Integer lastIndex = (Integer) centerWordView.getTag();
        return (lastIndex == null ? 0 : lastIndex) + 1;
    }

    final int countInStrip = mSuggestionsCountInStrip;
    mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip);
    int x = 0;
    for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) {
        if (positionInStrip != 0) {
            final View divider = mDividerViews.get(positionInStrip);
            // Add divider if this isn't the left most suggestion in suggestions strip.
            addDivider(stripView, divider);
            x += divider.getMeasuredWidth();
        }

        final int width = getSuggestionWidth(positionInStrip, stripWidth);
        final TextView wordView = layoutWord(positionInStrip, width);
        stripView.addView(wordView);
        setLayoutWeight(wordView, getSuggestionWeight(positionInStrip), ViewGroup.LayoutParams.MATCH_PARENT);
        x += wordView.getMeasuredWidth();

        if (SuggestionStripView.DBG) {
            layoutDebugInfo(positionInStrip, placerView, x);
        }
    }
    return startIndexOfMoreSuggestions;
}

From source file:com.mobiletin.inputmethod.indic.suggestions.SuggestionStripLayoutHelper.java

/**
 * Layout suggestions to the suggestions strip. And returns the start index of more
 * suggestions./*  w ww. j a v  a2 s.c  o m*/
 *
 * @param suggestedWords suggestions to be shown in the suggestions strip.
 * @param stripView the suggestions strip view.
 * @param placerView the view where the debug info will be placed.
 * @return the start index of more suggestions.
 */
public int layoutAndReturnStartIndexOfMoreSuggestions(final SuggestedWords suggestedWords,
        final ViewGroup stripView, final ViewGroup placerView) {

    if (suggestedWords.isPunctuationSuggestions()) {
        return layoutPunctuationsAndReturnStartIndexOfMoreSuggestions((PunctuationSuggestions) suggestedWords,
                stripView);
    }
    final int startIndexOfMoreSuggestions = setupWordViewsAndReturnStartIndexOfMoreSuggestions(suggestedWords,
            mSuggestionsCountInStrip);
    final TextView centerWordView = mWordViews.get(mCenterPositionInStrip);
    final int stripWidth = stripView.getWidth();
    final int centerWidth = getSuggestionWidth(mCenterPositionInStrip, stripWidth);
    if (suggestedWords.size() == 1 || getTextScaleX(centerWordView.getText(), centerWidth,
            centerWordView.getPaint()) < MIN_TEXT_XSCALE) {
        // Layout only the most relevant suggested word at the center of the suggestion strip
        // by consolidating all slots in the strip.
        final int countInStrip = 1;
        mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip);
        layoutWord(mCenterPositionInStrip, stripWidth - mPadding);
        stripView.addView(centerWordView);
        setLayoutWeight(centerWordView, 1.0f, ViewGroup.LayoutParams.MATCH_PARENT);
        if (SuggestionStripView.DBG) {
            layoutDebugInfo(mCenterPositionInStrip, placerView, stripWidth);
        }
        final Integer lastIndex = (Integer) centerWordView.getTag();
        return (lastIndex == null ? 0 : lastIndex) + 1;
    }

    //  final int countInStrip = mSuggestionsCountInStrip;

    //
    // Changes apply here
    //
    // Increase the strip size coloum

    final int countInStrip = mSuggestionsCountInStrip;

    mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip);
    int x = 0;
    for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) {
        if (positionInStrip != 0) {
            final View divider = mDividerViews.get(positionInStrip);
            // Add divider if this isn't the left most suggestion in suggestions strip.
            addDivider(stripView, divider);
            x += divider.getMeasuredWidth();
        }

        final int width = getSuggestionWidth(positionInStrip, stripWidth);
        final TextView wordView = layoutWord(positionInStrip, width);

        stripView.addView(wordView);
        setLayoutWeight(wordView, getSuggestionWeight(positionInStrip), ViewGroup.LayoutParams.MATCH_PARENT);
        x += wordView.getMeasuredWidth();

        if (SuggestionStripView.DBG) {
            layoutDebugInfo(positionInStrip, placerView, x);
        }
    }
    return startIndexOfMoreSuggestions;
}