jclova
jclova

Reputation: 5576

Android:TextView height doesn't change after shrinking the font size

When I click on the button, the font size shrinks to 12. However, the result is :

enter image description here

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:background="#80ff0000"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

java:

public class FontSizeTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TextView text = (TextView) findViewById(R.id.test);




        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                text.setTextSize(12);
            }
        });
    }
}

How do I shrink the height of the textView so that it only wraps the actual font?

Upvotes: 18

Views: 11536

Answers (7)

dvircn
dvircn

Reputation: 357

So, After a long search I have found a solution. Every time you set text do:

setText("I am a Text",TextView.BufferType.SPANNABLE);

Or after resizing your text just do:

setText(getText(),TextView.BufferType.SPANNABLE);

Upvotes: 23

Moritz
Moritz

Reputation: 10352

I found that adding the character "\u200b" does not appear as a broken character, nor does it add a whitespace/linebreak.

For more details on the "ZERO WIDTH SPACE" see: http://www.fileformat.info/info/unicode/char/200b/index.htm

Upvotes: 3

safexy
safexy

Reputation: 51

Instead of using:

final String DOUBLE_BYTE_SPACE = "\u3000";
text.setText(text + DOUBLE_BYTE_SPACE);

Better use:

final String DOUBLE_BYTE_WORDJOINER = "\u2060";
text.setText(text + DOUBLE_BYTE_WORDJOINER);

Extra information: Word Joiner is 0 width space.

Upvotes: 5

jclova
jclova

Reputation: 5576

Finally, I found the reason/solution!!!

This is a known bug for Android 3.1+

Issue 17343

Issue 22493

Possible workaround are:

text.setText(text+"\n");

or

final String DOUBLE_BYTE_SPACE = "\u3000";
text.setText(text + DOUBLE_BYTE_SPACE);

Upvotes: 5

Orlymee
Orlymee

Reputation: 2357

try calling invalidate() or a variant of invalidate(). From android developer docs:

public void invalidate ()

Since: API Level 1 Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

http://developer.android.com/reference/android/view/View.html#invalidate()

Upvotes: 1

Satya
Satya

Reputation: 340

In main.xml file TextView size given android:textSize="40sp" . and in side java, click on the button change the value here

  public void onClick(View v) {

            text.setTextSize(12);
        }

Upvotes: 0

andymal
andymal

Reputation: 554

Maybe setting the view to View.GONE, changing your text size, then setting to View.VISIBLE would work?

Upvotes: 1

Related Questions