1

When we have a TextView with a text of a certain size, it seems that if we add padding to the enclosing layout then the text wraps around to remain the same font size.
But does this mean that setting specific fontsize could cause issues in different screen sizes etc?
Because I was expecting somehow the text would be "resized" to stay in one line even if the font size was affected. Also it seems that if my text gets to have more characters the line will also wrap around and mess my layout.
How could I use then font and not mess up my layout if the characters displayed are more?

Update:
Basically my question is, how do we deal with TextViews that can take a string of arbitrary length and using fonts appropriately?

Jim
  • 17,102
  • 31
  • 115
  • 227

2 Answers2

2

Generally speaking, I find that setting the layout_width and/or layout_height to wrap_content meets most of my needs. There are situations where the text length can get too long, though. In this case, there are a few different strategies that people use. The one you choose depends on your layout and what you are trying to achieve.

Dynamic resizing of text

You mentioned font size in your question so I will cover it first. You can use different values or dimensions to change the font size for different device sizes and orientations.

Doing this doesn't necessarily make the font fit the TextView, though. So some people try to resize the font programmatically. This doesn't seem to be a very mainstream solution in Android but here is a discussion:

Truncate the text

If the text is too long to fit in the TextView's layout and it isn't necessary to see the whole thing you can just cut it off at the end. You also have several options with android:ellipsize. Read these for more details:

Use a ScrollView

When I have a TextView that can potentially contain a lot of text I usually put it in a ScrollView. See this answer for more details:

Community
  • 1
  • 1
Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
0

Write code like this.

At first declare a LinearLayout then declare a TextView

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_weight="1">

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#000000"
          android:gravity="center"
          android:text="Hi I am Jay"
          android:textSize="16sp" />
</LinearLayout>

Here gravity for LinearLayout denotes the TextView is in the center of LinearLayout and the gravity fot TextView denotes the text will be in center of TextView. You can change the textSize as 18sp or 20sp by writing this in textSize field.

Avishek Das
  • 531
  • 1
  • 5
  • 19