I am trying to display a textual histogram into a TextView in Android Studio but I don’t want it to break the line of characters and separate them onto two lines, because it obviously ruins the histogram. I have the text set to resize itself in order to fit into the text box when I have more and more lines, but I want to make the size scale to the longest line specifically, that way they are all on a single line.
Here is what the histogram is showing, rather than adjusting the line size
<TextView
android:id="@+id/histogramTextbox"
android:layout_width="0dp"
android:layout_height="453dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:autoSizeMaxTextSize="80sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/totalRollsText" />
Here is what I have in my textview XML in order to scale the font size.
2
Answers
Create constraintlayout as parent of a textview. Set your textview width to match constraint. You are good to go.
For whatever reason, you have to set
android:maxLines
too in order to get the autosizing to work properly (and it’s a good idea to useapp:autoSize*
instead ofandroid:autoSize*
to support API levels < 26 or if you are usingAppCompat
components).There are a lot of details about getting this right here – some key takeaways in addition to using
maxLines
are: do not useandroid:singleLine
, and do not usewrap_content
for either width or height.Demo Use
If the max lines changes dynamically, you can set it in code instead, like this