Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am using this version of auto fit text view, which works great:</p> <pre><code>public class AutoFitTextView extends TextView { public AutoFitTextView(Context context) { super(context); init(); } public AutoFitTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { maxTextSize = this.getTextSize(); if (maxTextSize &lt; 35) { maxTextSize = 30; } minTextSize = 20; } private void refitText(String text, int textWidth) { if (textWidth &gt; 0) { int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight(); float trySize = maxTextSize; this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize); while ((trySize &gt; minTextSize) &amp;&amp; (this.getPaint().measureText(text) &gt; availableWidth)) { trySize -= 1; if (trySize &lt;= minTextSize) { trySize = minTextSize; break; } this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize); } this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize); } } @Override protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) { refitText(text.toString(), this.getWidth()); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w != oldw) { refitText(this.getText().toString(), w); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int parentWidth = MeasureSpec.getSize(widthMeasureSpec); refitText(this.getText().toString(), parentWidth); } public float getMinTextSize() { return minTextSize; } public void setMinTextSize(int minTextSize) { this.minTextSize = minTextSize; } public float getMaxTextSize() { return maxTextSize; } public void setMaxTextSize(int minTextSize) { this.maxTextSize = minTextSize; } private float minTextSize; private float maxTextSize; } </code></pre> <p>also set the width to:</p> <pre><code>android:layout_width="match_parent" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload