Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit content of textview after change text size

Tags:

android

I have text view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/item_table_light_grey"
          android:gravity="center"
          android:orientation="vertical" >

    <TextView
            android:id="@android:id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            />
</LinearLayout>

When I change size of text using code

TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setTextSize(adapter.getTextSize());

width and height of textView not changed though text size changed successfully. What am I doing wrong?

like image 221
Shox Avatar asked Oct 21 '25 04:10

Shox


1 Answers

Android does NOT refresh layout of views with "wrap_content" once it has been displayed.

So if you add a child view, or modify the content dynamically, you're screwed.

To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with "wrap_content". The code and instructions to use are available here:

https://github.com/ea167/android-layout-wrap-content-updater

Another solution is to set fixed values to layout_width and layout_height, add gravity="center" on the TextView, and remove the wrap_content

Hope it helps!

like image 170
Eric Avatar answered Oct 27 '25 05:10

Eric



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!