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

The following examples show how to use android.widget.TextView#measure() . 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: DefaultIconGenerator.java    From google-maps-clustering with Apache License 2.0 6 votes vote down vote up
@NonNull
private BitmapDescriptor createClusterIcon(int clusterBucket) {
    @SuppressLint("InflateParams")
    TextView clusterIconView = (TextView) LayoutInflater.from(mContext)
            .inflate(R.layout.map_cluster_icon, null);
    clusterIconView.setBackground(createClusterBackground());
    clusterIconView.setTextColor(mIconStyle.getClusterTextColor());
    clusterIconView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
            mIconStyle.getClusterTextSize());

    clusterIconView.setText(getClusterIconText(clusterBucket));

    clusterIconView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    clusterIconView.layout(0, 0, clusterIconView.getMeasuredWidth(),
            clusterIconView.getMeasuredHeight());

    Bitmap iconBitmap = Bitmap.createBitmap(clusterIconView.getMeasuredWidth(),
            clusterIconView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(iconBitmap);
    clusterIconView.draw(canvas);

    return BitmapDescriptorFactory.fromBitmap(iconBitmap);
}
 
Example 2
Source File: TextSharedElementCallback.java    From CoolSignIn with Apache License 2.0 6 votes vote down vote up
@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;
    }

    // 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();
}
 
Example 3
Source File: RemoteControlView.java    From RemoteControlView with Apache License 2.0 6 votes vote down vote up
private void init(Context context) {
    setWillNotDraw(false);
    mPhonePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mBackPath = new Path();
    // 不使用硬件加速,否则虚线显示不出
    setLayerType(LAYER_TYPE_SOFTWARE, null);
    // 拖拽有效区域
    frameLayout = new FrameLayout(context);
    frameLayout.setBackgroundColor(Color.parseColor(CONTENT_COLOR));
    frameLayout.setOnDragListener(this);
    addView(frameLayout);
    // 提示文字
    mTextView = new TextView(context);
    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
    mTextView.setTextColor(Color.WHITE);
    mTextView.setText("长按并拖拽下方按钮到这里");
    LayoutParams fl = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    fl.gravity = Gravity.CENTER;
    mTextView.setLayoutParams(fl);
    mTextView.measure(0, 0);
    addView(mTextView);
}
 
Example 4
Source File: TranslateLanguagePanel.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Measures how large the view needs to be to avoid truncating its children. */
public void measureWidthRequiredForView() {
    mMinimumWidth = 0;

    final int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

    FrameLayout layout = new FrameLayout(getContext());
    TextView estimator = (TextView) LayoutInflater.from(getContext()).inflate(
            R.layout.infobar_text, null);
    layout.addView(estimator);
    for (int i = 0; i < getCount(); ++i) {
        estimator.setText(getStringForLanguage(i));
        estimator.measure(spec, spec);
        mMinimumWidth = Math.max(mMinimumWidth, estimator.getMeasuredWidth());
    }
}
 
Example 5
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 6
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 7
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 8
Source File: StickyHeaderBehaviorTest.java    From brickkit-android with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }

    context = InstrumentationRegistry.getTargetContext();
    recyclerView = mock(RecyclerView.class);
    adapter = mock(BrickRecyclerAdapter.class);
    view = mock(View.class);
    dataManager = mock(BrickDataManager.class);

    when(dataManager.getBrickRecyclerAdapter()).thenReturn(adapter);
    when(adapter.getRecyclerView()).thenReturn(recyclerView);

    itemView = new TextView(context);
    ((TextView) itemView).setText(TEST);
    itemView.measure(MOCK_VIEW_SIZE, MOCK_VIEW_SIZE);

    stickyViewHolder = new BrickViewHolder(itemView);

    stickyHolderContainer = spy((ViewGroup) LayoutInflater.from(context).inflate(R.layout.vertical_header, new LinearLayout(context), false));
    stickyHolderContainer.layout(0, 0, MOCK_VIEW_SIZE, MOCK_VIEW_SIZE);
    stickyHolderContainer.setLayoutParams(new ViewGroup.LayoutParams(MOCK_VIEW_SIZE, MOCK_VIEW_SIZE));

    View header = spy((ViewGroup) LayoutInflater.from(context).inflate(R.layout.text_brick, new LinearLayout(context), false));
    header.layout(0, 0, MOCK_VIEW_SIZE, MOCK_VIEW_SIZE);
    header.setLayoutParams(new ViewGroup.LayoutParams(MOCK_VIEW_SIZE, MOCK_VIEW_SIZE));

    stickyHolderLayout = ((ViewGroup) stickyHolderContainer.findViewById(R.id.header_sticky_layout));
    stickyHolderLayout.addView(header);

    headerBehavior = spy(new TestStickyHeaderBehavior(dataManager, stickyHolderContainer));
    headerBehavior.swapStickyView(stickyViewHolder);
    headerBehavior.translateStickyView();
    headerBehavior.setStickyBackgroundColor(Color.WHITE);
}
 
Example 9
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 10
Source File: AppIntroHolder.java    From miappstore with Apache License 2.0 5 votes vote down vote up
/**
 * 获取10行文本的高度
 * @return
 */
public int getShortMeasureHeight() {
    TextView textView = new TextView(UiUtils.getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
    textView.setLines(10);//表示有十行数据
    //开始宽度
    int width = tv_content.getMeasuredWidth();
    //指定测量规则
    int measureWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int measureHeight = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.AT_MOST);
    //开始测量
    textView.measure(measureWidth, measureHeight);
    return textView.getMeasuredHeight();
}
 
Example 11
Source File: Game.java    From Simple-Solitaire with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a textView and add it to the given layout (game content). Used to add custom texts
 * to a game. This also sets the text apperance to AppCompat and the gravity to center.
 * The width and height is also measured, so you can use it directly.
 *
 * @param width   The width to apply to the
 * @param layout  he textView will be added to this layout
 * @param context Context to create view
 */
protected void addTextViews(int count, int width, RelativeLayout layout, Context context) {

    for (int i = 0; i < count; i++) {
        TextView textView = new TextView(context);
        textView.setWidth(width);
        TextViewCompat.setTextAppearance(textView, R.style.TextAppearance_AppCompat);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(textViewColor);
        layout.addView(textView);
        textView.measure(0, 0);
        textViews.add(textView);
    }
}
 
Example 12
Source File: AutoCompletePanel.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int getItemHeight() {
    if (_h != 0)
        return _h;

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView item = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, null);
    item.measure(0, 0);
    _h = item.getMeasuredHeight();
    return _h;
}
 
Example 13
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 14
Source File: NoPaddingTextView.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
private int getAdditionalPadding() {
    float textSize = getTextSize();

    TextView textView = new TextView(getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    textView.setLines(1);
    textView.measure(0, 0);
    int measuredHeight = textView.getMeasuredHeight();
    if (measuredHeight - textSize > 0) {
        mAdditionalPadding = (int) (measuredHeight - textSize);
        Log.v("NoPaddingTextView", "onMeasure: height=" + measuredHeight + " textSize=" + textSize + " mAdditionalPadding=" + mAdditionalPadding);
    }
    return mAdditionalPadding;
}
 
Example 15
Source File: NoPaddingTextView.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
private int measureHeight(String text, int widthMeasureSpec) {
    float textSize = getTextSize();

    TextView textView = new TextView(getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    textView.setText(text);
    textView.measure(widthMeasureSpec, 0);
    return textView.getMeasuredHeight();
}
 
Example 16
Source File: LemonBubbleInfo.java    From LemonBubble4Android with MIT License 5 votes vote down vote up
/**
 * 获取指定的textV的行高
 *
 * @param textView 要获取的textView的行高
 * @return textView的每行的高度
 */
int getTitleHeight(TextView textView, int viewWidth) {
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(_PST.dpToPx(viewWidth), View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return _PST.pxToDp(textView.getMeasuredHeight());
}
 
Example 17
Source File: PropertyDisplayer.java    From remoteyourcam-usb with Apache License 2.0 4 votes vote down vote up
public void setPropertyDesc(int[] values, String[] valuesStrings, Integer[] icons) {
    this.values = values;
    toggleButton.setEnabled(editable && values.length > 0);

    if (icons != null) {
        Drawable drawables[] = new Drawable[icons.length];
        for (int i = 0; i < icons.length; ++i) {
            if (icons[i] == null) {
                continue;
            }
            Drawable d = context.getResources().getDrawable(icons[i]);
            drawables[i] = d;
            if (biggestValueWidth == -1) {
                biggestValueWidth = d.getIntrinsicWidth();
                layoutListView(biggestValueWidth);
            }
        }
        adapter = new PropertyAdapter<Drawable>(context, R.layout.property_icon_list_item, drawables);
    } else {
        String longest = "";
        int longestLength = 0;
        for (String s : valuesStrings) {
            int len = s.length();
            if (len > longestLength) {
                longest = s;
                longestLength = len;
            }
        }
        TextView t = new TextView(context);
        // HACK HACK
        if (context.getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE)) {
            t.setTextAppearance(context, android.R.style.TextAppearance_Large);
        } else {
            t.setTextAppearance(context, android.R.style.TextAppearance_Medium);
        }
        t.setText(longest + " ");
        t.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        biggestValueWidth = t.getMeasuredWidth();
        layoutListView(biggestValueWidth);
        adapter = new PropertyAdapter<String>(context, R.layout.property_list_item, valuesStrings);
    }
    list.setAdapter(adapter);

    if (toggleButton.isChecked()) {
        if (values.length == 0) {
            listContainer.setVisibility(View.GONE);
        } else {
            listContainer.setVisibility(View.VISIBLE);
            updateCurrentPosition();
        }
    } else {
        dataChanged = true;
    }
}
 
Example 18
Source File: LemonHelloInfo.java    From LemonHello4Android with MIT License 4 votes vote down vote up
private int measureTextViewHeight(TextView textView, int viewWidth) {
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(_PST.dpToPx(viewWidth), View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return _PST.pxToDp(textView.getMeasuredHeight());
}
 
Example 19
Source File: PropertyDisplayer.java    From remoteyourcam-usb with Apache License 2.0 4 votes vote down vote up
public void setPropertyDesc(int[] values, String[] valuesStrings, Integer[] icons) {
    this.values = values;
    toggleButton.setEnabled(editable && values.length > 0);

    if (icons != null) {
        Drawable drawables[] = new Drawable[icons.length];
        for (int i = 0; i < icons.length; ++i) {
            if (icons[i] == null) {
                continue;
            }
            Drawable d = context.getResources().getDrawable(icons[i]);
            drawables[i] = d;
            if (biggestValueWidth == -1) {
                biggestValueWidth = d.getIntrinsicWidth();
                layoutListView(biggestValueWidth);
            }
        }
        adapter = new PropertyAdapter<Drawable>(context, R.layout.property_icon_list_item, drawables);
    } else {
        String longest = "";
        int longestLength = 0;
        for (String s : valuesStrings) {
            int len = s.length();
            if (len > longestLength) {
                longest = s;
                longestLength = len;
            }
        }
        TextView t = new TextView(context);
        // HACK HACK
        if (context.getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE)) {
            t.setTextAppearance(context, android.R.style.TextAppearance_Large);
        } else {
            t.setTextAppearance(context, android.R.style.TextAppearance_Medium);
        }
        t.setText(longest + " ");
        t.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        biggestValueWidth = t.getMeasuredWidth();
        layoutListView(biggestValueWidth);
        adapter = new PropertyAdapter<String>(context, R.layout.property_list_item, valuesStrings);
    }
    list.setAdapter(adapter);

    if (toggleButton.isChecked()) {
        if (values.length == 0) {
            listContainer.setVisibility(View.GONE);
        } else {
            listContainer.setVisibility(View.VISIBLE);
            updateCurrentPosition();
        }
    } else {
        dataChanged = true;
    }
}
 
Example 20
Source File: ValuePicker.java    From NumberPicker with GNU General Public License v3.0 4 votes vote down vote up
public static int getTextViewHeight(Context context, boolean isBig, float textSize, float textSizeSelected) {
    TextView textView = getTextView(context, isBig, textSize, textSizeSelected);
    textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    return textView.getMeasuredHeight();
}