I use new autosize feature added in support library 26. I read a documentation which can be found here : https://developer.android.com/preview/features/autosizing-textview.html
I suppose that it should work this way: You can enable auto-sizing with this attribute: app:autoSizeTextType="uniform"
. I think that TextView
should use all available space to display a whole text (not just a part - it shouldn't be cropped) and the textSize
should be as big as possible. If you need to limit a maximum or minimum size of the text then you can use these two attributes:
app:autoSizeMinTextSize="XXsp" // (you can also use px or dp values.)
or
app:autoSizeMaxTextSize="XXsp"
So far so good. Let's say that I need a TextView with 56dp width. I have texts with a different length and I want to set these texts to this TextView
. It should be automatically resized so it displays the whole text (all characters + not cropped) in the biggest possible textSize
.
This is my TextView:
<android.support.v7.widget.AppCompatTextView android:id="@+id/vName" style="@style/TextView.AutoSize" android:layout_width="56dp" android:gravity="bottom|center_horizontal" android:maxLines="1" app:autoSizeMinTextSize="1px" app:autoSizeTextType="uniform" app:layout_constraintBottom_toTopOf="@id/vGuideline" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" />
Unfortunately, the text view is cropped. I wanted to set this String as a text to the TextView above: "Groupa" but this is the result:
(TextView is inside ConstraintLayout with yellow circle background.)
As you can see the textview is not resized at all. Do you have any idea what to do?
Thanks.
You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!
Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.
In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.
Additional to the other correct answers I found another point which prevents autosizing to work.
Do not use android:singleLine="true"
together with autosizing. Use the newer android:maxLines="1"
instead.
I have tested this for a few situations, and have the below conclusion:
You must have bounded width and height. For example, if you set width to be match_parent
but wrap_content
for height, I think Android doesn't know that how high you want to stretch your text. In your example you don't have a specific height, so I think that's why it doesn't work.
For example:
I don't know why Android official document would use wrap_content as an example...
And as you can see I didn't use other attributes in my example, so it probably is not the problem of incorrect attributes.
And, yes, the TextView I am using is android.support.v7.widget.AppCompatTextView.
And as long as you are using support library 26.0.0 or above it is good enough.
EDIT:
As for ConstraintLayout
, the principal is the same. You should have both bounded width and height, which means either one of below for each dimension:
You have specified an absolute value for that dimension (width or height)
You have set Constraint to both directions
For example:
UPDATE: (2017-09-21)
I have tested that unfortunately it seems it does not support custom typeface yet, which is a function published together in support library v26...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With