How to make textView wrap its multiline content exactly?

android, android-layout, textview

Solution

I have faced this problem and didn't find the solution in internet. I did this trick by creating the new component TightTextView that remeasures the given text in case you have specified the maxWidth of the component and the width of Layout (of text) is less that the measured width of the view.

package com.client.android.app.views;

import android.content.Context;
import android.text.Layout;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Tightly wraps the text when setting the maxWidth.
 * @author sky
 */
public class TightTextView extends TextView {
    private boolean hasMaxWidth;

    public TightTextView(Context context) {
        this(context, null, 0);
    }

    public TightTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TightTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (hasMaxWidth) {
            int specModeW = MeasureSpec.getMode(widthMeasureSpec);
            if (specModeW != MeasureSpec.EXACTLY) {
                Layout layout = getLayout();
                int linesCount = layout.getLineCount();
                if (linesCount > 1) {
                    float textRealMaxWidth = 0;
                    for (int n = 0; n < linesCount; ++n) {
                        textRealMaxWidth = Math.max(textRealMaxWidth, layout.getLineWidth(n));
                    }
                    int w = Math.round(textRealMaxWidth);
                    if (w < getMeasuredWidth()) {
                        super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST),
                                heightMeasureSpec);
                    }
                }
            }
        }
    }


    @Override
    public void setMaxWidth(int maxpixels) {
        super.setMaxWidth(maxpixels);
        hasMaxWidth = true;
    }

    @Override
    public void setMaxEms(int maxems) {
        super.setMaxEms(maxems);
        hasMaxWidth = true;
    }
}

!!! Just did port it to older android APIs, cuz getMaxWidth() is only available since API level 16.

Problem

How can I make the textview wrap such text exactly ? android:width attribute is not a solution, because the text is dynamic. Desired behaviour ``` |Adcs | |adscfd| ``` Current behavour: ``` |Adcs | |adscfd | ``` Hereis the code (styles of TextViews only define things like textColor, textSize, textStyle). ``` <TextView android:id="@+id/text_title_holder" style="@style/TextBold.Black.Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:maxWidth="100dp" android:maxLines="2" android:text="Adcs adscfd" android:gravity="left" android:visibility="visible" /> ``` The topic wrap_content width on mutiline TextView has no good answer.

Original source