Last Updated
Viewed 08 Times
     

I'm trying to make the view bounds of a TextView fit exactly to the size of the text within. I have fiddled around with it, trying to make a custom TextView to solve it, but couldn't get a solid solution.

Here are some resources that were useful but didn't quite do the trick for me:

Is there a solid, reusable solution to this? Thank you!

My problem is, when i turn on a setting in the developer options to see view bounds, i can see that the orange "20" text wraps much larger space than it is requried. (top and bottom)

I tried to set: android:includeFontPadding="false" but it is just simple push up the content, leaving a big empty space bottom of the text's container.

How can i remove the extra space?

enter image description here

XML:

 <LinearLayout
    android:id="@+id/topDash"
    android:layout_width="180dp"
    android:layout_height="180dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/circle_gray"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/RobotoTextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/sog_C"
        android:textColor="@color/blue_medium"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/RobotoTextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="20"
        android:textColor="@color/orange"
        android:textSize="100sp" />

    <TextView
        android:id="@+id/robotoTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="KM/H"
        android:textColor="@color/gray_darker"
        android:textSize="24sp" />
</LinearLayout>

Similar Question 2 : Android Textview content size

Is it possible to have a TextView object containing text of two sizes?


We can change the color of text content using single TextView object using textViewObj.setText(Html.fromHtml()); method. But is it possible to have text of two sizes in the single TextView object like below

like this.

What I am getting is shown below. I have tried a lot but the text is of same size.

like this

I'm using an Android auto-sizing TextView via the appcompat library, as below:

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/info"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:maxLines="1"
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="8sp"
    app:autoSizeMaxTextSize="14sp"
    app:autoSizeStepGranularity="0.5sp"
    android:text="@string/something" />

This works nicely the first time the text is set - eg the text size shrinks if it needs to.

But the second time it's set, the text size doesn't grow again if the content is small enough to allow it. It remains at the smaller size appropriate for the previous content.

So, what is the proper procedure for calling setText on an auto-sizing TextView, so that the auto-sizing parameters are respected? Or is there a way to force the TextView to re-evaluate its content and resize the text?

I have tried setting the text size back to the default before calling setText but that seems to have no effect.

Similar Question 5 (1 solutions) : onCreate/textView sizing issue

Similar Question 6 (1 solutions) : Changing text of TextView based on its size

Similar Question 8 (1 solutions) : auto-sizing a textview?

cc