Home > Software engineering >  Prevent text from wrapping into a new line in Android Studio
Prevent text from wrapping into a new line in Android Studio

Time:02-02

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.

CodePudding user response:

Create constraintlayout as parent of a textview. Set your textview width to match constraint. You are good to go.

CodePudding user response:

I'd like to comment, but not enough reputation, so you may want to add "single line" property and I 'm not sure if ellipsize is not an option for you,

you may want to add: android:ellipsize="end" & android:singleLine="true" to your textView.

<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:ellipsize="end"
        android:singleLine="true"
        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" />
  •  Tags:  
  • Related