출처 : http://ankri.de/autoscale-textview/
I was in need of a TextView that has a fixed with but dynamic content, so I needed the content to automatically scale.
The AutoScaleTextView
takes the android:textSize
value and uses it as the preferred text size. You can also set the minimum text size. The default for the minimum text size is 10dp. TheAutoScaleTextView
now tries to take the maximum value between the preferred and minimum size that fits into the Views
width (with regarding the padding).
Here is the result:
You can see that the AutoScaleTextView
automatically scales the text to fit in the Views
width. For the different views I used layout_width: 130dp
and layout_height=30dp
, textSize: 16dp
and 10dp
for the minTextSize
. On the second Ice Cream Sandwich TextView
I used 13dp
for the minTextSize
and layout_height: wrap_content
.
Get the source and explanation after the break.
The code itself is pretty simple. I improved a snippet I found on stackoverflow.com. I sadly cannot remember the exact post.
You can checkout the demo project: https://bitbucket.org/ankri/autoscaletextview/src
Or download the project: https://bitbucket.org/ankri/autoscaletextview/downloads
Please feel free to leave feedback or report bugs here:https://bitbucket.org/ankri/autoscaletextview/issues?status=new&status=open
Or view the code below:
The important part is the refitText(String, int)
method. Note: If you are dealing with big textsizes you should set the treshold to a higher value.
Here is a snippet from the main.xml
I used in the demo project
The values from the res/values/dimens.xml
are 100dp
for the width
and 30dp
for the height
.
The content of the res/values/attrs.xml
to alter the minTextSize
in the layout files is:
And the content of the res/values/styles.xml
to set the default minTextSize
or other default styles for the AutoScaleTextView
Have fun using the code. I uploaded the code under the Beerware License. You can take the code and do whatever you want with it, as long as you mention me somewhere in your code, or your final app. If we ever meet and you think that the code, or explanation was helpful, feel free to buy me a beer and have a chat.
Or you could help a fellow out by donating some money.
Updated licence
When I uploaded this code I never thought anyone would use it, but since some people actually are using it, I updated the licence to Apache 2.0. But still… If you’re in the mood to buy me a beer. Please do
'Android > GUI & Activity' 카테고리의 다른 글
Animation 종류 (0) | 2014.10.28 |
---|---|
[펌[] Android ActionBar 활용 (0) | 2014.10.22 |
AutoScaleTextView 추천 소스 (0) | 2014.10.16 |
CustomView lifeCycle (0) | 2014.09.30 |
리스트뷰 밑의 내용 스크롤시 애니메이션 붙이는 방법 (0) | 2014.09.25 |