Support
Quality
Security
License
Reuse
kandi has reviewed android-autofittextview and discovered the below as its top functions. This is intended to give you an instant insight into android-autofittextview implemented functionality, and help decide if they suit your requirements.
A TextView that automatically resizes text to fit perfectly within its bounds.
Usage
dependencies {
compile 'me.grantland:autofittextview:0.2.+'
}
License
Copyright 2014 Grantland Chew
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Android - set text size programatically cuts off words
//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());
}
QUESTION
Android - set text size programatically cuts off words
Asked 2017-Sep-01 at 10:00So 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>
ANSWER
Answered 2017-Sep-01 at 10:00I 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());
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit