Problem : While adding text to a text view in android , when text is long enough to make textview height greater than screen, the textview is only increasing its height up to the screen height and not going beyond the screen.
Requirement: To increase the height of text view according to the text content . Is it possible to change the height of a text view at runtime?
EDIT Even though I change the height of TV (greater than height of screen) in XML still my TV height is not exceeding screen height.
CodePudding user response:
The AutofitTextView library from MavenCentral handles this nicely. The source hosted on Github(1k stars) at https://github.com/grantland/android-autofittextview
app/build.Gradle
repositories {
mavenCentral()
}
dependencies {
compile 'me.grantland:autofittextview:0.2. '
}
Enable any View extending TextView in code:
AutofitHelper.create(textView);
Enable any View extending TextView in XML:
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
</me.grantland.widget.AutofitLayout>
Use the built-in Widget in code or XML:
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
CodePudding user response:
With Android 8.0 (API level 26) and higher, you can instruct a TextView to let the text size expand or contract automatically to fill its layout based on the TextView's characteristics and boundaries. This setting makes it easier to optimize the text size on different screens with dynamic content
Your textview.
<TextView
android:id="@ id/word_content_numericaltext"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:autoSizeMaxTextSize="70sp" // add this for max text size
android:autoSizeMinTextSize="30sp" // add this for min text size
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="word"
android:textAlignment="center"
android:textColor="@color/darkblue"
android:textSize="30sp" />
Note: If you set autosizing in an XML file, it is not recommended to use the value "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results.
Refer here for more details : autosizing_textview