title: Auto-fit TextView for Android
body: <h2>Background</h2>
  
  <p>Many times we need to auto-fit the font of the TextView to the boundaries given to it.</p>
  
  <h2>The problem</h2>
  
  <p>Sadly, even though there are many threads and posts (and suggested solutions) talking about this problem (example <a href="https://stackoverflow.com/questions/2617266/how-to-adjust-text-font-size-to-fit-textview">here</a>, <a href="https://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds">here</a> and <a href="https://bitbucket.org/ankri/autoscaletextview/src" rel="noreferrer">here</a>), none of them actually work well.</p>
  
  <p>That's why, I've decided to test each of them till I find the real deal.</p>
  
  <p>I think that the requirements from such a textView should be:</p>
  
  <ol>
  <li><p>Should allow using any font, typeface, style, and set of characters.</p></li>
  <li><p>Should handle both width and height</p></li>
  <li><p>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.</p></li>
  <li><p>Should allow multi-line or single-line. In case of multi-line, allow max &amp; min lines.</p></li>
  <li><p>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.</p></li>
  <li><p>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.</p></li>
  </ol>
  
  <h2>What I've tried</h2>
  
  <p>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.</p>
  
  <p>I've made a sample project that allows me to visually see if the TextView auto-fits correctly.</p>
  
  <p>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.</p>
  
  <p>Here's the code (also available <a href="https://mega.co.nz/#!w9QRyBaB!J3KdnyTuNXRsYH9Hk5f4nBBXkRM1IAQnV8-53ckRooY" rel="noreferrer">here</a>):</p>
  
  <h3>File <code>res/layout/activity_main.xml</code></h3>
  
  <pre><code>&lt;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"&gt;
    &lt;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" /&gt;
    &lt;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" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <h3>File <code>src/.../MainActivity.java</code></h3>
  
  <pre><code>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&lt;textLength;++i)
        builder.append(ALLOWED_CHARACTERS.charAt(_random.nextInt(ALLOWED_CHARACTERS.length())));
      return builder.toString();
      }
    }
  </code></pre>
  
  <h2>The question</h2>
  
  <p>Does anybody know of a solution for this common problem that actually work?</p>
  
  <p>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.</p>
  
  <hr>
  
  <h2>GitHub project</h2>
  
  <p>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, <a href="https://github.com/AndroidDeveloperLB/AutoFitTextView" rel="noreferrer"><strong>here</strong></a>.</p>