title: How to auto-adjust text size on a multi-line TextView according to the view max dimensions?
body: <p>I've been searching for a way of auto fit a text inside a textview. Through my search I've found many solutions like:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2617266/how-to-adjust-text-font-size-to-fit-textview">FontFitTextView</a></li>
  <li><a href="https://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds?lq=1">AutoResizeTextView</a></li>
  <li><a href="https://stackoverflow.com/questions/2596452/how-to-scale-resize-text-to-fit-a-textview?lq=1">AutoScale/Resize</a></li>
  </ul>
  
  <p>But like many others these doesn't solve my problem.
  They don't work as expected when we use a TextView with multiline.</p>
  
  <p>Basically my goal is this one:</p>
  
  <p><img src="https://i.stack.imgur.com/3QImF.png" alt="Phase 1">
  <img src="https://i.stack.imgur.com/Y97ZO.png" alt="Phase 2">
  <img src="https://i.stack.imgur.com/GJBtg.png" alt="Phase 3">
  <img src="https://i.stack.imgur.com/tIrOl.png" alt="Phase 4"></p>
  
  <p>As you can see, the text resizes based on width, height and also pays attention to the line break, creating a multi-line textview. Also be able to change the typeface.</p>
  
  <p>One of my ideas to solve this was something like this:</p>
  
  <pre><code>int size = CONSTANT_MAX_SIZE;
  TextView tv = (TextView) findViewById(R.id.textview1)
  while(Math.abs(tv.getMeasuredHeight()) &gt;= TEXTVIEW_MAX_HEIGHT) {
      size--;
      tv.setTextSize(size);
      tv.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
      i++;
  }
  </code></pre>
  
  <p>CONSTANT_MAX_SIZE is a constant that defines the max size of the font (textsize propriety in a TextView)</p>
  
  <p>TEXTVIEW_MAX_HEIGHT is a constant that defines the max size that the textview can have.</p>
  
  <p>This was called every time the text in the textview was changed.</p>
  
  <p>The textview xml was something like this:</p>
  
  <pre><code>&lt;TextView
       android:id="@+id/textview1"
       android:layout_width="200dp"
       android:layout_height="wrap_content"
       android:singleLine="false"
       android:inputType="textMultiLine"
       android:text=""
       android:textSize="150sp" /&gt;
  </code></pre>
  
  <p>Since the width would be limited in the XML, only the height of the view needs to be considered since adjusting it android would automatically create a multiline when needed.</p>
  
  <p>Although this is a potential solution isn't working perfectly (far from it) and it doesn't support resize down (when you delete text).</p>
  
  <p>Any sugestions and/or ideas?</p>