Background
Many times we need to auto-fit the font of the TextView to the boundaries given to it.
The problem
Sadly, even though there are many threads and posts (and suggested solutions) talking about this problem (example here, here and here), none of them actually work well.
That’s why, I’ve decided to test each of them till I find the real deal.
I think that the requirements from such a textView should be:
-
Should allow using any font, typeface, style, and set of characters.
-
Should handle both width and height
-
No truncation unless text cannot fit because of the limitation, we’ve
given to it (example: too long text, too small available size). However, we could request for horizontal/vertical scrollbar if we wish, just for those cases. -
Should allow multi-line or single-line. In case of multi-line, allow max & min lines.
-
Should not be slow in computation. Using a loop for finding the best size? At least optimize it and don’t increment your sampling by 1 each time.
-
In case of multi-line, should allow to prefer resizing or using more lines, and/or allow to choose the lines ourselves by using the “\n” character.
What I’ve tried
I’ve tried so many samples (including those of the links, I’ve written about), and I’ve also tried to modify them to handle the cases, I’ve talked about, but none really work.
I’ve made a sample project that allows me to visually see if the TextView auto-fits correctly.
Currently, my sample project only randomize the text (the English alphabet plus digits) and the size of the textView, and let it stay with single line, but even this doesn’t work well on any of the samples I’ve tried.
Here’s the code (also available here):
File res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:text="Button" />
<FrameLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_above="@+id/button1"
android:layout_alignParentLeft="true" android:background="#ffff0000"
android:layout_alignParentRight="true" android:id="@+id/container"
android:layout_alignParentTop="true" />
</RelativeLayout>
File src/.../MainActivity.java
public class MainActivity extends Activity
{
private final Random _random =new Random();
private static final String ALLOWED_CHARACTERS ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup container=(ViewGroup)findViewById(R.id.container);
findViewById(R.id.button1).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(final View v)
{
container.removeAllViews();
final int maxWidth=container.getWidth();
final int maxHeight=container.getHeight();
final FontFitTextView fontFitTextView=new FontFitTextView(MainActivity.this);
final int width=_random.nextInt(maxWidth)+1;
final int height=_random.nextInt(maxHeight)+1;
fontFitTextView.setLayoutParams(new LayoutParams(width,height));
fontFitTextView.setSingleLine();
fontFitTextView.setBackgroundColor(0xff00ff00);
final String text=getRandomText();
fontFitTextView.setText(text);
container.addView(fontFitTextView);
Log.d("DEBUG","width:"+width+" height:"+height+" text:"+text);
}
});
}
private String getRandomText()
{
final int textLength=_random.nextInt(20)+1;
final StringBuilder builder=new StringBuilder();
for(int i=0;i<textLength;++i)
builder.append(ALLOWED_CHARACTERS.charAt(_random.nextInt(ALLOWED_CHARACTERS.length())));
return builder.toString();
}
}
The question
Does anybody know of a solution for this common problem that actually work?
Even a solution that has much less features that what I’ve written about, for example one that has just a constant number of lines of text, and adjusts its font according to its size, yet never have weird glitches and having the text get too large/small compared to its available space.
GitHub project
Since this is such an important TextView, I’ve decided to publish a library, so that everyone could easily use it, and contribute to it, here.