Text does not adapt to TextView - android

I need to insert a large text in a TextView, but when pasting the text it does not adapt to the size of the TextView, because it has exceeded the width of the TextView and the text is giant, how to fix this?
Note: I need to correct it using the Android Studio visual interface.

To set optimal size of the text in TextView You can use autoSizeTextType property. To do it Your TextView must have fixed sizes or match_parent. Then You can use autoSizeTextType It looks something like this:
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="#string/lorem_ipsum"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
/>
Results:
TextView height : 150dp
TextView height : 300dp
Here is a short video on YouTube with great explanation

Related

Why do I need to set layout_height when maxHeight and minHeight are set

I want the height of my TextView to auto-size its layout_height when max/min text size is hit.
Problem: In order to auto-size my text, I am forced to add a layout_height. There are cases where the text is one or two words and cases where there are many words. When there are only one or two words my text hits the autoSizeMaxTextSize but the layout_height remains at 200dp and then my view looks like there is a big space between my TextView (the title) and the lower view (the body) because I set a static height for layout_height. When I have less text I do not want my TextView's height to take up the full 200dp but to scale down to the minHeight instead.
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:minHeight="100dp"
android:maxHeight="200dp"
android:ellipsize="end"
android:gravity="start"
android:maxLength="70"
android:maxLines="4"
android:text="#{title.trim()}"
android:textAlignment="textStart"
android:textColor="?android:textColorPrimary"
android:textStyle="bold"
android:textSize="42sp"
app:autoSizeMaxTextSize="42sp"
app:autoSizeMinTextSize="8sp"
app:autoSizeTextType="uniform"/>
I have tried using android:layout_height="wrap_content" in conjunction with android:minHeight="100dp" and android:maxHeight="200dp" and the auto text resizing. The result was to wrap content with no text resizing and no maxHeight.
Does anyone know how to accomplish this?
Because when the view's parent lays it out and figures out how much space to give it, it doesn't know about those fields. It only knows about the layout_ fields. It sizes it completely based on that and on the layout rules of the parent. min and max text size is only used by the view itself when deciding what font size to use.
Ah, you asked a different question in the body. If you want the size of the text view to change based on length of text, use wrap_content for height. To put a max height, put it inside a view with layout_height 200, or use contraint rules in a ConstraintLayout to set a max height.

Auto adjust text size to fit the layout Android

Is there's way to change the text size to fit screen-width ?
i have Header with long title, the problem is on small size screen that title goes to another line. i would like to change font size to fit the whole text in 1 line
Android officialy supports autosizing.for implementing dynamic size you should set autoSizeTextType attribute to uniform and set autoSizeMaxTextSize and autoSizeMinTextSize attributes to your desired size.
*attention : for apis below 26 you should use 'app' prefix for attributes instead of 'android' .
and finally you should have a textView like this for stretching text to screen-width:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Hello, how are you today"
android:textSize="100sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeMinTextSize="12sp"
app:autoSizeStepGranularity="2sp"
app:autoSizeTextType="uniform"
/>
TextViewCompat from AndroidX is the official solution for your problem.

Can I scale the actual text in a TextView in Android?

Is it possible to create a scaled TextView like this where the text itself is scaled in one direction? In the picture, the top half shows a basic TextView outlined in blue. The bottom half shows the same TextView after the scaling I'm trying to do. The height is the same but the width of the view has been cut in half.
I don't think that this is possible with a TextView using the default font. To accomplish the effect you are looking for you would probably have better luck creating and resizing an image or dynamically using a different font that has half the width per character.
I personally use the library autofittextview from grantland in my projects.
The usage is simple.
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>
for example. The result is as follows.

Is there a way to perfectly center text?

I wanted to know if it was possible to create a custom TextView that centers text perfectly, no matter what the font is. That's my major issue right now, but I'm also wondering if it's possible to set the specific height using pixels so that the height would also be consistent.
This picture shows how different fonts are sized and centered. The longest black line in the picture is the middle of the white space. Letters in the picture are the same in every way except for the fonts. The text size is the same (text.setTextSize(TypedValue.COMPLEX_UNIT_PX, 450);), and they're all centered. I hope someone knows the answer to these questions! If you need any code from my app, just ask. Thanks!
EDIT: I am aware of android:gravity="veritcal_center", but that only works to an extent. What you see above is that implemented in the textview, but each font has a different center of gravity, so android:gravity="veritcal_center" wouldn't really make all of these center perfectly along the screen. I'm looking for a way to create a custom textView that somehow measures the height of text and centers it with those parameters. A suggestion by #vmironov in the comments works, but only if the textview has one character. I have not been able to mess around with his code, but I will when I get a chance and I'll post here if I find anything. Thanks!
A simple way to achieve what you want is to use following code:
<LinearLayout
android:layout_width="100dp"
android:layout_height="20dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/your_text" />
</LinearLayout>
set the height and width values of LinearLayout fixed if you want to set the text to be alligned in center within constant height and width
Set the gravity of the view to "center_vertical". Something like :
android:gravity="center_vertical"
You should be able to center your text by applying a gravity attribute to the containing TextView.
In XML you would assign an attribute to your TextView, which would look like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/some_text" />
If you do want to specify a particular size for your text, it is strongly suggested you use SP (Scaled Pixels) as your unit of measurement, and not Pixels. You could also set an Appearance Attribute for your TextView to control the size, which is also shown in the code example.

Custom typeface text size varies greatly between fonts

I have a few TextViews in my app where I use a custom typeface. I have given the view centered layout gravity to get the text centered in its parent layout. It looks fine in the previewer with the Sans font, but when I change the typeface, the result looks like the text is shifted down, like this:
Compared to this:
Here is the xml for that element:
<FrameLayout android:id="#+id/Turn_ScoreA_Frame" android:padding="5dp" android:background="#color/teamA_secondary" android:layout_margin="5dp" android:layout_gravity="center" android:layout_height="50dp" android:layout_width="110dp">
<android.view.View android:id="#+id/Turn_ScoreABG" android:background="#color/teamA_primary" android:layout_height="fill_parent" android:layout_width="fill_parent"></android.view.View>
<TextView android:id="#+id/Turn_ScoreA" android:includeFontPadding="false" android:text="5" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="32dp"></TextView>
</FrameLayout>
This also occurs when I use fill_parent for layout_width and height of the text view, with text gravity center. I set the custom Typeface in the OnCreate of the view in which this layout is used, just by using TextView.SetTypeface.
Does anyone know what could be causing this? I'm having trouble tracking anything down on this.
As a note, I've worked around this on several views by setting the margin_bottom to -10dp or so, but I'd like to remove that, and I can't get that hack to work on confined views like this one, anyways.
The font is Anton, by the way.
EDIT: This is definitely the result of the text being too large for its container. The thing is, Sans fits just fine, and the new font would fit but its measured size is too large. What I'm hoping is to find a way to get the text to remain at its current visible size and fit in the center of the container, in such a way that doesn't feel too hacky =)
You need to add:
android:includeFontPadding="false"
You have 3 options
Decrease the text size
Increase the height of FrameLayout
Change the font which can fit correctly
you cannot change the property of the font, it is designed is such a way that it has empty space above it, which doesn't fit in your given android:layout_height="50dp" when android:textSize="32dp"

Categories

Resources