Example usage for android.widget TextView getPaint

List of usage examples for android.widget TextView getPaint

Introduction

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

Prototype

public TextPaint getPaint() 

Source Link

Document

Gets the TextPaint used for the text.

Usage

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 ww  w . ja va2 s .c  om*/
 *
 * @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./*from ww  w . j a v a  2 s  .  co  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;
}

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:com.taobao.weex.extend.module.actionsheet.WXActionSheet.java

private void ensureTitle() {
    if (!TextUtils.isEmpty(titleText) || !TextUtils.isEmpty(messageText)) {
        LinearLayout titleContainer = new LinearLayout(getActivity());
        titleContainer.setOrientation(LinearLayout.VERTICAL);
        titleContainer.setGravity(Gravity.CENTER);
        titleContainer.setBackgroundColor(Color.WHITE);
        titleContainer//from   w w  w. j  ava  2  s. com
                .setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp2px(56)));

        if (!TextUtils.isEmpty(titleText)) {
            TextView title = new TextView(getActivity());
            title.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
            title.setText(titleText);
            title.setGravity(Gravity.CENTER_HORIZONTAL);
            title.setTextColor(COLOR_TITLE);
            title.getPaint().setFakeBoldText(true);
            titleContainer.addView(title);
        }

        if (!TextUtils.isEmpty(messageText)) {
            TextView message = new TextView(getActivity());
            message.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            message.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            message.setGravity(Gravity.CENTER_HORIZONTAL);
            message.setText(messageText);
            message.setTextColor(COLOR_TITLE);
            titleContainer.addView(message);
        }

        titleContainer.setClickable(true);
        titleContainer.setFocusableInTouchMode(true);

        sheetContainer.addView(titleContainer);
        sheetContainer.addView(createLine());
    }
}

From source file:com.ns.developer.tagview.widget.TagCloudLinkView.java

/**
 * tag draw/* w  w w .j  a v  a2  s  . c o  m*/
 */
public void drawTags() {

    if (!mInitialized) {
        return;
    }

    // clear all tag
    removeAllViews();

    // layout padding left & layout padding right
    float total = getPaddingLeft() + getPaddingRight();
    // ???index
    int index = 1;
    // ?
    int pindex = index;

    // List Index
    int listIndex = 0;
    for (String item : mTags) {
        final int position = listIndex;
        final String tag = item;

        // inflate tag layout
        View tagLayout = (View) mInflater.inflate(R.layout.tag, null);
        tagLayout.setId(index);
        //            tagLayout.setBackgroundColor(mTagLayoutColor);

        // tag text
        TextView tagView = (TextView) tagLayout.findViewById(R.id.tag_txt);
        tagView.setText(tag);
        tagView.setPadding(INNER_VIEW_PADDING, INNER_VIEW_PADDING, INNER_VIEW_PADDING, INNER_VIEW_PADDING);
        tagView.setTextColor(mTagTextColor);
        tagView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectListener != null) {
                    mSelectListener.onTagSelected(tag, position);
                }
            }
        });

        // calculateof tag layout width
        float tagWidth = tagView.getPaint().measureText(tag) + INNER_VIEW_PADDING * 2; // tagView padding (left & right)

        // deletable text
        TextView deletableView = (TextView) tagLayout.findViewById(R.id.delete_txt);
        if (mIsDeletable) {
            deletableView.setVisibility(View.VISIBLE);
            deletableView.setText(DEFAULT_DELETABLE_STRING);
            deletableView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDeleteListener != null) {
                        String targetTag = tag;
                        TagCloudLinkView.this.remove(position);
                        mDeleteListener.onTagDeleted(targetTag, position);
                    }
                }
            });
            tagWidth += deletableView.getPaint().measureText(DEFAULT_DELETABLE_STRING) + INNER_VIEW_PADDING * 2; // deletableView Padding (left & right)
        } else {
            deletableView.setVisibility(View.GONE);
        }

        LayoutParams tagParams = new LayoutParams(HEIGHT_WC, HEIGHT_WC);
        tagParams.setMargins(0, 0, 0, 0);

        if (mWidth <= total + tagWidth + LAYOUT_WIDTH_OFFSET) {
            tagParams.addRule(RelativeLayout.BELOW, pindex);
            tagParams.topMargin = TAG_LAYOUT_TOP_MERGIN;
            // initialize total param (layout padding left & layout padding right)
            total = getPaddingLeft() + getPaddingRight();
            pindex = index;
        } else {
            tagParams.addRule(RelativeLayout.ALIGN_TOP, pindex);
            tagParams.addRule(RelativeLayout.RIGHT_OF, index - 1);
            if (index > 1) {
                tagParams.leftMargin = TAG_LAYOUT_LEFT_MERGIN;
                total += TAG_LAYOUT_LEFT_MERGIN;
            }
        }
        total += tagWidth;
        addView(tagLayout, tagParams);
        index++;
        listIndex++;
    }

    LayoutParams tagParams = new LayoutParams(HEIGHT_WC, HEIGHT_WC);
    tagParams.setMargins(0, 0, 0, 0);

    if (isAutoCompleteMode || !mIsDeletable) {
        return;
    }

    int tagWidth = 50; //25dp

    if (mWidth <= total + tagWidth + LAYOUT_WIDTH_OFFSET) {
        tagParams.addRule(RelativeLayout.BELOW, pindex);
        tagParams.topMargin = TAG_LAYOUT_TOP_MERGIN;
        // initialize total param (layout padding left & layout padding right)
        total = getPaddingLeft() + getPaddingRight();
        pindex = index;
    } else {
        tagParams.addRule(RelativeLayout.ALIGN_TOP, pindex);
        tagParams.addRule(RelativeLayout.RIGHT_OF, index - 1);
        if (index > 1) {
            tagParams.leftMargin = TAG_LAYOUT_LEFT_MERGIN;
            total += TAG_LAYOUT_LEFT_MERGIN;
        }
    }
    total += tagWidth;
    View tagLayout = (View) mInflater.inflate(R.layout.tag, null);
    addView(tagLayout, tagParams);
    tagLayout.findViewById(R.id.add_image_view).setVisibility(VISIBLE);
    tagLayout.findViewById(R.id.tag_txt).setVisibility(GONE);
    tagLayout.setClickable(true);
    tagLayout.setFocusable(true);
    tagLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mAddTagListener != null) {
                mAddTagListener.onAddTag();
            }
        }
    });
    int transparentColor = ContextCompat.getColor(getContext(), android.R.color.transparent);
    tagLayout.setBackgroundColor(transparentColor);

}

From source file:com.youqu.piclbs.util.SlidingTabLayout.java

private void updateTabStyles() {
    for (int i = 0; i < mTabCount; i++) {
        View v = mTabsContainer.getChildAt(i);
        //            v.setPadding((int) mTabPadding, v.getPaddingTop(), (int) mTabPadding, v.getPaddingBottom());
        TextView tv_tab_title = (TextView) v.findViewById(R.id.tv_tab_title);
        if (tv_tab_title != null) {
            tv_tab_title.setTextColor(i == mCurrentTab ? mTextSelectColor : mTextUnselectColor);
            tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
            tv_tab_title.setPadding((int) mTabPadding, 0, (int) mTabPadding, 0);
            if (mTextAllCaps) {
                tv_tab_title.setText(tv_tab_title.getText().toString().toUpperCase());
            }//  ww  w  .ja  va  2s.co  m

            if (mTextBold) {
                tv_tab_title.getPaint().setFakeBoldText(mTextBold);
            }
        }
    }
}

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

/**
 * Format appropriately the suggested word in {@link #mWordViews} specified by
 * <code>positionInStrip</code>. When the suggested word doesn't exist, the corresponding
 * {@link TextView} will be disabled and never respond to user interaction. The suggested word
 * may be shrunk or ellipsized to fit in the specified width.
 *
 * The <code>positionInStrip</code> argument is the index in the suggestion strip. The indices
 * increase towards the right for LTR scripts and the left for RTL scripts, starting with 0.
 * The position of the most important suggestion is in {@link #mCenterPositionInStrip}. This
 * usually doesn't match the index in <code>suggedtedWords</code> -- see
 * {@link #getPositionInSuggestionStrip(int,SuggestedWords)}.
 *
 * @param positionInStrip the position in the suggestion strip.
 * @param width the maximum width for layout in pixels.
 * @return the {@link TextView} containing the suggested word appropriately formatted.
 */// w  w w.j a v  a  2 s .c  om
private TextView layoutWord(final int positionInStrip, final int width) {
    final TextView wordView = mWordViews.get(positionInStrip);
    final CharSequence word = wordView.getText();
    if (positionInStrip == mCenterPositionInStrip && mMoreSuggestionsAvailable) {
        // TODO: This "more suggestions hint" should have a nicely designed icon.
        wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, mMoreSuggestionsHint);
        // HACK: Align with other TextViews that have no compound drawables.
        wordView.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight());
    } else {
        wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
    }
    // {@link StyleSpan} in a content description may cause an issue of TTS/TalkBack.
    // Use a simple {@link String} to avoid the issue.
    wordView.setContentDescription(TextUtils.isEmpty(word) ? null : word.toString());
    final CharSequence text = getEllipsizedText(word, width, wordView.getPaint());
    final float scaleX = getTextScaleX(word, width, wordView.getPaint());
    wordView.setText(text); // TextView.setText() resets text scale x to 1.0.
    wordView.setTextScaleX(Math.max(scaleX, MIN_TEXT_XSCALE));
    // A <code>wordView</code> should be disabled when <code>word</code> is empty in order to
    // make it unclickable.
    // With accessibility touch exploration on, <code>wordView</code> should be enabled even
    // when it is empty to avoid announcing as "disabled".
    wordView.setEnabled(
            !TextUtils.isEmpty(word) || AccessibilityUtils.getInstance().isTouchExplorationEnabled());
    return wordView;
}

From source file:com.hm.library.resource.tabstrip.PagerSlidingTabStrip.java

private void updateTabStyles() {

    for (int i = 0; i < tabCount; i++) {

        View v = tabsContainer.getChildAt(i);

        v.setBackgroundResource(tabBackgroundResId);

        if (v instanceof TextView) {

            try {
                TextView tab = (TextView) v;
                tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
                tab.setTypeface(tabTypeface, tabTypefaceStyle);
                tab.setTextColor(tabTextColor);

                // setAllCaps() is only available from API 14, so the upper
                // case
                // is made manually if we are on a
                // pre-ICS-build
                if (textAllCaps) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        ((TextView) tab).setAllCaps(true);
                    } else {
                        tab.setText(tab.getText().toString().toUpperCase(locale));
                    }/*  w  ww  . java 2 s .  co  m*/
                }
                if (i == selectedPosition) {
                    tab.setTextColor(selectedTabTextColor);
                    tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, selectedTabTextSize);
                    tab.getPaint().setFakeBoldText(true);
                } else {
                    tab.getPaint().setFakeBoldText(false);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

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

/**
 * Format appropriately the suggested word in {@link #mWordViews} specified by
 * <code>positionInStrip</code>. When the suggested word doesn't exist, the corresponding
 * {@link TextView} will be disabled and never respond to user interaction. The suggested word
 * may be shrunk or ellipsized to fit in the specified width.
 *
 * The <code>positionInStrip</code> argument is the index in the suggestion strip. The indices
 * increase towards the right for LTR scripts and the left for RTL scripts, starting with 0.
 * The position of the most important suggestion is in {@link #mCenterPositionInStrip}. This
 * usually doesn't match the index in <code>suggedtedWords</code> -- see
 * {@link #getPositionInSuggestionStrip(int,SuggestedWords)}.
 *
 * @param positionInStrip the position in the suggestion strip.
 * @param width the maximum width for layout in pixels.
 * @return the {@link TextView} containing the suggested word appropriately formatted.
 *///from  ww w .  j  a v  a  2 s.co  m
private TextView layoutWord(final int positionInStrip, final int width) {

    //    final TextView wordView = mWordViews.get(positionInStrip);

    final TextView wordView = mWordViews.get(positionInStrip);

    final CharSequence word = wordView.getText();
    if (positionInStrip == mCenterPositionInStrip && mMoreSuggestionsAvailable) {
        // TODO: This "more suggestions hint" should have a nicely designed icon.
        wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, mMoreSuggestionsHint);
        // HACK: Align with other TextViews that have no compound drawables.
        wordView.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight());
    } else {
        wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
    }
    // {@link StyleSpan} in a content description may cause an issue of TTS/TalkBack.
    // Use a simple {@link String} to avoid the issue.
    wordView.setContentDescription(TextUtils.isEmpty(word) ? null : word.toString());

    final CharSequence text = getEllipsizedText(word, width, wordView.getPaint());
    final float scaleX = getTextScaleX(word, width, wordView.getPaint());
    wordView.setText(text); // TextView.setText() resets text scale x to 1.0.
    wordView.setTextScaleX(Math.max(scaleX, MIN_TEXT_XSCALE));
    // A <code>wordView</code> should be disabled when <code>word</code> is empty in order to
    // make it unclickable.
    // With accessibility touch exploration on, <code>wordView</code> should be enabled even
    // when it is empty to avoid announcing as "disabled".
    wordView.setEnabled(
            !TextUtils.isEmpty(word) || AccessibilityUtils.getInstance().isTouchExplorationEnabled());
    return wordView;
}

From source file:io.doist.datetimepicker.time.RadialTimePickerView.java

private void drawDebug(Canvas canvas) {
    // Draw outer numbers circle
    final float outerRadius = mCircleRadius[HOURS] * mNumbersRadiusMultiplier[HOURS];
    canvas.drawCircle(mXCenter, mYCenter, outerRadius, mPaintDebug);

    // Draw inner numbers circle
    final float innerRadius = mCircleRadius[HOURS] * mNumbersRadiusMultiplier[HOURS_INNER];
    canvas.drawCircle(mXCenter, mYCenter, innerRadius, mPaintDebug);

    // Draw outer background circle
    canvas.drawCircle(mXCenter, mYCenter, mCircleRadius[HOURS], mPaintDebug);

    // Draw outer rectangle for circles
    float left = mXCenter - outerRadius;
    float top = mYCenter - outerRadius;
    float right = mXCenter + outerRadius;
    float bottom = mYCenter + outerRadius;
    canvas.drawRect(left, top, right, bottom, mPaintDebug);

    // Draw outer rectangle for background
    left = mXCenter - mCircleRadius[HOURS];
    top = mYCenter - mCircleRadius[HOURS];
    right = mXCenter + mCircleRadius[HOURS];
    bottom = mYCenter + mCircleRadius[HOURS];
    canvas.drawRect(left, top, right, bottom, mPaintDebug);

    // Draw outer view rectangle
    canvas.drawRect(0, 0, getWidth(), getHeight(), mPaintDebug);

    // Draw selected time
    final String selected = String.format("%02d:%02d", getCurrentHour(), getCurrentMinute());

    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    TextView tv = new TextView(getContext());
    tv.setLayoutParams(lp);//  w w w .j  a v a 2  s.c  om
    tv.setText(selected);
    tv.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    Paint paint = tv.getPaint();
    paint.setColor(DEBUG_TEXT_COLOR);

    final int width = tv.getMeasuredWidth();

    float height = paint.descent() - paint.ascent();
    float x = mXCenter - width / 2;
    float y = mYCenter + 1.5f * height;

    canvas.drawText(selected, x, y, paint);
}