Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make textView wrap its multiline content exactly?

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.

like image 899
Anton Rutkevich Avatar asked Jun 06 '12 11:06

Anton Rutkevich


People also ask

How do you add multiple lines in TextView?

The maxLines attribute is used to set the maximum line a TextView widget can have. You can prevent the TextView from creating multiple lines by setting the maxLines value as 1 . When you use the maxLines attribute, the full text of your TextView will be truncated, leaving a gap that may cause confusion.

How do I Auto Resize TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.


2 Answers

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.

like image 123
sky Avatar answered Oct 13 '22 00:10

sky


This question is a little old now but I too had this problem where I wanted green text in a black box over a mapView and got around it by putting my textView in a RelativeLayout container. I then used padding to set the border size. The textView now hugs the text nicely. My outline in eclipse looks like this.

RelativeLayout
    mapview
    LinearLayout
        RelativeLayout
            textView1  << this is the box I want to hug the text
        imageView1
    RelativeLayout
    etc....

Hope this helps.

like image 24
kris Avatar answered Oct 12 '22 22:10

kris