Reputation: 671
So I am using a library for resizing the text to fit within bounds: https://github.com/grantland/android-autofittextview. What I am trying to do is to loop through an ArrayList of text view and find the lowest text size and then set that text size to every text view. My Java Code:
//Loop through every textview and get lowest size
float lowestTextSize = textViews.get(0).getTextSize();
for (TextView textView : textViews){
if(lowestTextSize > textView.getTextSize()){
lowestTextSize = textView.getTextSize();
}
}
lowestTextSize = lowestTextSize / getResources().getDisplayMetrics().scaledDensity;
//Loop through every textview and set them all to the lowest text size previously found
for (TextView textView : textViews){
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, lowestTextSize);
}
My problem is that after setting that size to every textview the size of the textview changes but not the size of the words itself, so I get words cut in half (exception making the word whos size I am using, the one resized by the AutoFit library):
Any idea on how to solve this?
My XML code:
<LinearLayout
android:id="@+id/highestSellingProductLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:gravity="center"
android:orientation="horizontal">
<me.grantland.widget.AutofitTextView
android:id="@+id/highestSellingProduct"
autofit:minTextSize="15sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxLines="1"
android:paddingBottom="5dp"
android:text="NA"
android:textAlignment="center"
android:textColor="@color/colorSuccess"
android:textSize="22sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<me.grantland.widget.AutofitTextView
autofit:minTextSize="15sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:maxLines="1"
android:paddingBottom="5dp"
android:text="Highest Selling"
android:textAlignment="center"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
Upvotes: 1
Views: 410
Reputation: 671
I have found the issue. To change the size of text in java using AutofitTextView I had to use autofitHelper on every textview. The following code worked for me:
//Loop through every textview and get lowest size
float lowestTextSize = textViews.get(0).getTextSize();
for (TextView textView : textViews){
if(lowestTextSize > textView.getTextSize()){
lowestTextSize = textView.getTextSize();
}
}
lowestTextSize = lowestTextSize / getResources().getDisplayMetrics().scaledDensity;
//Loop through every textview and set them all to the lowest text size previously found
for (TextView textView : textViews){
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, lowestTextSize);
AutofitHelper autofitHelper = AutofitHelper.create(textView);
autofitHelper.setTextSize(textView.getTextSize());
}
Upvotes: 1