Example usage for android.widget TextView measure

List of usage examples for android.widget TextView measure

Introduction

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

Prototype

public final void measure(int widthMeasureSpec, int heightMeasureSpec) 

Source Link

Document

This is called to find out how big a view should be.

Usage

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 .  jav a2s  . c om
    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

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);/*  ww w  . j a va2s . c o  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.nma.util.sdcardtrac.SearchableActivity.java

public static int getTextHeight(Context context, CharSequence text, int textSize, int deviceWidth) { //, Typeface typeface,int padding) {
    TextView textView = new TextView(context);
    textView.setPadding(0, 0, 0, 0);//from  ww  w  .ja  va  2 s .c  o m
    textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    textView.setText(text, TextView.BufferType.SPANNABLE);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return textView.getMeasuredHeight();
}

From source file:ecust.news.myWidgetTabPageIndicator.java

public void setCurrentItem(int position) {
    ///*from w  w w  .j a va 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:com.agenthun.readingroutine.utils.TextSharedElementCallback.java

@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements,
        List<View> sharedElementSnapshots) {
    TextView initialView = getTextView(sharedElements);

    if (initialView == null) {
        Log.w(TAG, "onSharedElementEnd: No shared TextView, skipping");
        return;/*from  ww w  .java 2 s  . c  o m*/
    }

    // Setup the TextView's end values.
    initialView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTargetViewTextSize);
    ViewUtils.setPaddingStart(initialView, mTargetViewPaddingStart);

    // Re-measure the TextView (since the text size has changed).
    int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    initialView.measure(widthSpec, heightSpec);
    initialView.requestLayout();
}

From source file:org.dalol.orthodoxmezmurmedia.utilities.widgets.AmharicKeyboardView.java

private void handleModifiers(List<String> typographyList) {
    modifiersContainer.removeAllViews();

    for (int i = 0; i < typographyList.size(); i++) {
        String typography = typographyList.get(i);
        TextView modifierKey = new TextView(getContext());
        modifierKey.setText(typography);
        modifierKey.setTextColor(Color.WHITE);
        modifierKey.setIncludeFontPadding(false);
        modifierKey.setTypeface(mCharTypeface, Typeface.BOLD);
        modifierKey.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        modifierKey.setBackgroundDrawable(
                ContextCompat.getDrawable(getContext(), R.drawable.keyboard_modifierkey_bg));
        modifierKey.setTextSize(18f);/* w ww.j  av a2 s .  c o m*/
        modifierKey.setGravity(Gravity.CENTER);

        modifierKey.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditText == null) {
                    return;
                }
                TextView textView = (TextView) v;
                if (!mEditText.isFocused())
                    mEditText.requestFocus();
                Editable editableText = mEditText.getText();
                int start = mEditText.getSelectionStart();
                if (start == 0)
                    return;

                CharSequence textViewText = textView.getText();
                if (mEnableModifierFlag) {
                    mEnableModifierFlag = false;
                    editableText.replace(start - 1, start, textViewText);
                } else {
                    editableText.insert(start, textViewText);
                }
            }
        });

        LayoutParams params = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
        int margin = getCustomSize(1.5f);
        params.setMargins(margin, margin, margin, margin);
        modifiersContainer.addView(modifierKey, params);
    }
}

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

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;/*  www.ja  va 2 s.c om*/
    }
    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());
}

From source file:net.naonedbus.card.impl.HoraireCard.java

private int getTextViewCount(final ViewGroup parent) {
    final TextView textView = (TextView) mLayoutInflater.inflate(R.layout.card_horaire_text, parent, false);
    final DateTime noon = new DateTime().withHourOfDay(12).withMinuteOfHour(00);
    final int padding = getContext().getResources().getDimensionPixelSize(R.dimen.padding_medium);
    textView.setText(FormatUtils.formatTimeAmPm(getContext(), mTimeFormat.format(noon.toDate())));

    final int specY = MeasureSpec.makeMeasureSpec(parent.getHeight(), MeasureSpec.UNSPECIFIED);
    final int specX = MeasureSpec.makeMeasureSpec(parent.getWidth(), MeasureSpec.UNSPECIFIED);
    textView.measure(specX, specY);

    return (parent.getWidth() - padding) / textView.getMeasuredWidth();
}

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

public int captureBitmapCache(String in) {
    TextView tv = (TextView) MyApp.context.findViewById(R.id.ocrTextview);
    String tvText = tv.getText().toString();
    float tvTextSize = tv.getTextSize();
    int tvColor = tv.getCurrentTextColor();
    Bitmap bitmap = null;//from www.ja v  a2 s.co m

    tv.setTextSize(36);
    tv.setTextColor(Color.CYAN);
    tv.setTypeface(Typeface.SANS_SERIF);

    tv.setText(in);

    while (bitmap == null) {
        // http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null
        tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());

        tv.setDrawingCacheEnabled(true);
        tv.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(tv.getDrawingCache());
        tv.destroyDrawingCache();
        tv.setDrawingCacheEnabled(false);
    }

    FileOutputStream fos = null;
    int res = -1;
    try {
        fos = new FileOutputStream(currentDirectory() + "/files/" + MyApp.SCREENSHOT_FILENAME);
        if (fos != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.close();
            res = 0;
        }
    } catch (Throwable e) {
        Log.i(MyApp.TAG, e.getMessage().toString());
        res = -1;
    }

    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvTextSize);
    tv.setTypeface(Typeface.MONOSPACE);
    tv.setText(tvText);
    tv.setTextColor(tvColor);
    return res;
}

From source file:com.mobicage.rogerthat.plugins.messaging.widgets.AdvancedOrderWidget.java

private void correctCountWidth(final View v, final AdvancedOrderCategoryItemRow row) {
    mActivity.getMainService().postOnUIHandler(new SafeRunnable() {
        @Override/*www .ja v  a 2s .  c o m*/
        protected void safeRun() throws Exception {
            TextView nameLbl = (TextView) v.findViewById(R.id.name);
            if (!row.name.equals(nameLbl.getText())) {
                return;
            }
            TextView countLbl = (TextView) v.findViewById(R.id.count);
            countLbl.measure(0, 0);
            int countWidth = countLbl.getMeasuredWidth();
            if (countWidth > 140) {
                ViewGroup.LayoutParams lpCount = countLbl.getLayoutParams();
                lpCount.width = 140;
                countLbl.setLayoutParams(lpCount);
                countLbl.requestLayout();
            }
        }
    });
}