[android] How to adjust text font size to fit textview

Is there any way in android to adjust the textsize in a textview to fit the space it occupies?

E.g. I'm using a TableLayout and adding several TextViews to each row. Since I don't want the TextViews to wrap the text I rather see that it lowers the font size of the content.

Any ideas?

I have tried measureText, but since I don't know the size of the column it seems troublesome to use. This is the code where I want to change the font size to something that fits

TableRow row = new TableRow(this);   
for (int i=0; i < ColumnNames.length; i++) {    
    TextView textColumn = new TextView(this);      
    textColumn.setPadding(0, 0, 1, 0);
    row.addView(textColumn, new TableRow.LayoutParams()); 
table.addView(row, new TableLayout.LayoutParams());  

This question is related to android font-size textview

The answer is

The solution below incorporates all of the suggestions here. It starts with what was originally posted by Dunni. It uses a binary search like gjpc's, but it is a bit more readable. It also include's gregm's bug fixes and a bug-fix of my own.

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

public class FontFitTextView extends TextView {

    public FontFitTextView(Context context) {

    public FontFitTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

    private void initialise() {
        mTestPaint = new Paint();
        //max size defaults to the initially specified text size unless it is too small

    /* Re size the font so the specified text fits in the text box
     * assuming the text box is the specified width.
    private void refitText(String text, int textWidth) 
        if (textWidth <= 0)
        int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
        float hi = 100;
        float lo = 2;
        final float threshold = 0.5f; // How close we have to be


        while((hi - lo) > threshold) {
            float size = (hi+lo)/2;
            if(mTestPaint.measureText(text) >= targetWidth) 
                hi = size; // too big
                lo = size; // too small
        // Use lo so that we undershoot rather than overshoot
        this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo);

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int height = getMeasuredHeight();
        refitText(this.getText().toString(), parentWidth);
        this.setMeasuredDimension(parentWidth, height);

    protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
        refitText(text.toString(), this.getWidth());

    protected void onSizeChanged (int w, int h, int oldw, int oldh) {
        if (w != oldw) {
            refitText(this.getText().toString(), w);

    private Paint mTestPaint;

Similar questions with android tag:

Similar questions with font-size tag:

Similar questions with textview tag: