shoe
shoe

Reputation: 1070

How to force a TextView to change width to the width of the text in Android

I have hints set on my TextViews which have android:layout_width="wrap_content" set and if the text that is typed into the TextView is not as wide as the width of the hint-text, then the TextView won't change its width to wrap the text. However, if the text that is entered is wider than the width of the hint-text, and given there is room for the TextView to widen, then it will grow to wrap the text.

Ideally, what I would like is for the TextView to shrink to my text regardless of what the width of the hint-text was (the original width of the TextView), but I will also except answers that provide a way to get the width of my text, so I can just call setWidth on the TextView with that width.

I've already tried changing the minimum width of the TextView to 0 after initializing it, but that doesn't solve the problem. The funny thing is if I enter text that is wider than the hint-text (forcing it to grow), it will shrink if I take away characters from that text until it gets back to the width that the hint-text forced it to have. What I want is for it to always shrink to the width of the text, regardless of the width of the hint-text.

Here's my layout:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <TextView
            android:id="@+id/month_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="mm"
            android:textColorHint="@color/hint_color"
            android:singleLine="true"
            android:textSize="40sp"
            android:maxLength="2"/>
        <TextView
            android:id="@+id/first_hypen_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="-"
            android:singleLine="true"
            android:textSize="40sp"/>
        <TextView
            android:id="@+id/day_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="dd"
            android:textColorHint="@color/hint_color"
            android:singleLine="true"
            android:textSize="40sp"
            android:maxLength="2"/>
        <TextView
            android:id="@+id/second_hyphen_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="-"
            android:singleLine="true"
            android:textSize="40sp"/>
        <TextView
            android:id="@+id/year_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="yyyy"
            android:textColorHint="@color/hint_color"
            android:singleLine="true"
            android:textSize="40sp"
            android:maxLength="4"/>
    </LinearLayout>

Upvotes: 1

Views: 2647

Answers (2)

the beest
the beest

Reputation: 483

What I ended up doing was to set the hint on the TextView to "" when it had text in it, and then resetting the hint to the original hint if all the text was ever deleted from it. This way, once text was entered, whatever settings were forcing the TextView to have a minimum width -- because of the initial hint-text width -- would be reconfigured after the new hint was set, and the new minimum TextView width set to 0 because of the new hint-text width (""); and if all the text was deleted the hint would still show as expected.

Upvotes: 2

Ready Android
Ready Android

Reputation: 3622

Use "ems" with hint length

e.g. In XML design

android:ems="2"

Here for you day_textview has hint "dd", it's length is 2 then android:ems="2"

OR, In custom code

TextView tvDay = (TextView) findViewById(R.id.day_textview);
tvDay.setEms(tvDay.getHint().length());

So "ems" it turns out refers to the size of the widest character, typically an "M", get it? So setting minEms to an integer value say 3, on an EditText or TextView should ensure it's at least 3 characters wide. You can set the maxEms as well.

The thing I understood from you is, you want a text with hint characters length width. When you enter more text than hint length then your text should shrink till that entered text length. And once it's done and you remove all the entered text then again it should take the same hint's length width size.

For that above information is given.

Test e.g.

Add in you XML layout:

<EditText
        android:id="@+id/edtCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:hint="abc"
        android:maxLines="1"
        android:text="I am an Android Developer" />

Add this to your activity:

EditText edt = (EditText) findViewById(R.id.edtCheck);
        edt.setEms(3);//3 just for test
        edt.setMaxEms(1000);//1000 just for test

Now run and check is it works exact for what you are looking.

Upvotes: 1

Related Questions