Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing TextSize of TextView and then Decreasing It Doesn't return to normal height of view element

I'm fairly new to Android and would never have gotten as far as I am without this forum. Here's my problem: Am using a simple LinearLayout with an ImageView vertically positioned above a TextView. My goal is to display a series of screens with a images on the top and a short text caption on the bottom. The text is wrappable and the image should be scaled to fill the remaining available vertical space.

Everything works fine if I keep the text font size constant (I use .setTextSize(35) normally). The problem is that when I display an empty image, I choose to enlarge the text (.setTextSize(120)). That works OK..but then when I next display a subsequent image and revert to the text size to 35, there's a large unfilled gap between the bottom of the image and the top of the now smaller text area.

What appears to be happening is that using the larger text size once has somehow permanently increased the minimum height of the TextView. I've tried clearing the TextView (.setText("")..changing the size to very small (.setTextSize(12)...and endlessly fiddling with the LinearLayout parameters (weight, gravity)...to no avail. Any thoughts on how to fix this would be most welcome.

<ImageView android:id="@+id/img" android:layout_width="match_parent"
    android:layout_height="0dp" android:layout_weight="1.0" android:layout_gravity="fill_vertical" android:contentDescription="@string/desc" />

<TextView android:id="@+id/caption" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_weight="0" android:layout_gravity="bottom"
    android:gravity="center"/>
like image 387
user2495677 Avatar asked Nov 18 '25 17:11

user2495677


1 Answers

***Solution discovered after posting: Turns out that this is a known bug since Android 3.1. See Android:TextView height doesn't change after shrinking the font size.

Of the recommended solutions/workarounds listed there, the one that I'm liking is that 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);

like image 114
user2495677 Avatar answered Nov 21 '25 07:11

user2495677



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!