0

I'm trying to resize the wrapping text in a TextView to fit within its bounds. I've created a new class with the source from here and I've added it inside my layout:

            <com.util.AutoResizeTextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_alignLeft="@id/ivBulb"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="80dp"
                android:layout_below="@id/ivBulb"
                android:layout_marginRight="10dp"
                android:id="@+id/textP"
                android:ellipsize="none"
                android:fontFamily="sans-serif"
                android:textColor="#000"
                android:textSize="100sp"/> <!--maximum size -->

The text that should be displayed inside the TextView is something like:

The sunset filled the entire sky with the deep color of rubies, setting the clouds ablaze. The waves crashed and danced along the shore, moving up and down in a graceful and gentle rhythm like they were dancing.

Some texts can be a multiline but some don't. My problem is that the text font size is small (about 35dp) and it's always the same :( I've added a ScrollView in order to be able to display all the text but I want to keep just the TextView and the text to resize

I'm new in Android development so please be gentle.

Dana
  • 1,788
  • 3
  • 21
  • 51
  • why do you need to fit the text if you have it in a Scrollview? – Mohammad Ersan Jul 24 '17 at 09:03
  • I added the scrollview because my text was the same but I don't want to use it. It should be just a simple textbox – Dana Jul 24 '17 at 09:06
  • ScrollView height is infinite, so the children can have as height as they need, then your AutoResizeTextView will not calculate its content correctly to resize it – Mohammad Ersan Jul 24 '17 at 09:09
  • I first used it without a ScrollView but the text was the same. I set the text programmatically when the activity starts, that's why it can vary. I have some texts that can be bigger than the TextView that's why I added a ScrollView. I'll edit my question :) – Dana Jul 24 '17 at 09:27

1 Answers1

0

use from android.support.v7.widget.AppCompatTextView and set

android:ellipsize="none"
                                android:singleLine="true"
                                app:autoSizeMaxTextSize="14sp"
                                app:autoSizeMinTextSize="12sp"
                                app:autoSizeStepGranularity="0.5sp"
                                app:autoSizeTextType="uniform"

in

<android.support.v7.widget.AppCompatTextView
                                android:ellipsize="none"
                                android:singleLine="true"
                                app:autoSizeMaxTextSize="14sp"
                                app:autoSizeMinTextSize="12sp"
                                app:autoSizeStepGranularity="0.5sp"
                                app:autoSizeTextType="uniform"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"              android:id="@+id/txt_ortopedi" 
                                android:textColor="@color/text_color"
                                android:padding="4dp" android:hint="@string/search"
                                android:gravity="clip_vertical|center_horizontal" android:layout_gravity="center"
                                android:background="@color/white"/>
Mr_Moradi
  • 290
  • 3
  • 8