12

Say I have a TextView of a particular size (doesn't really matter what... fill_parent, 20dip, whatever). Is it possible to tell the text to shrink/grow in size to fit the available space without doing a lot of math?

Jeremy Logan
  • 45,614
  • 37
  • 119
  • 143
  • Please see the answer here, works quite well: [enter link description here][1] [1]: http://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds – AllDayAmazing May 21 '15 at 20:45

4 Answers4

18

Well, it's a little simpler than "a lot of complex maths", but there's a hack solution I use for this that's workable if the width of your text isn't too far off from the width of your textview.

    // HACK to adjust text width
    final int INITIAL_TEXTSIZE = 15;
    final int TEXTVIEW_WIDTH = textviewBackground.getIntrinsicWidth(); // I use the background image to find the width, but you can use a fixed value or whatever other method you prefer.
    final String text = ...;
    textView.setText( text );

    int size = INITIAL_TEXTSIZE;
    while( textview.getPaint().measureText(text) > TEXTVIEW_WIDTH )
        textview.setTextSize( --size );

If the width of your text can be significantly different than the width of your TextView, it might be better to employ a binary search instead of a linear search to find the right font size.

Not saying this is an ideal solution, but it's an expedient one if you don't often need to adjust your textsize. If you need to frequently adjust your textsize, it might be better to do some complex maths.

emmby
  • 95,927
  • 63
  • 178
  • 243
2

I tried out almost all of the above and most failed including the top one. The simplest and most effective I found was from emmby but found that you had to set the initial textsize. Here is an improvement on his where you don't need to specify any textsize at all:

  final String text = "HELP";

  TextView tvFlash = (TextView) findViewById(R.id.tvFlash);

  DisplayMetrics metrics = getResources().getDisplayMetrics();
  int width = metrics.widthPixels;
  int height = metrics.heightPixels;

  tvFlash.setText(text);
  tvFlash.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1);

  int size = 1; 

  do
  {
    float textWidth = tvFlash.getPaint().measureText(text);

    if (textWidth < width)
      tvFlash.setTextSize(++size);
    else
    {
      tvFlash.setTextSize(--size);
      break;
    }
  }while(true);
AndroidDev
  • 20,063
  • 26
  • 131
  • 216
0

U can calculate the idea font size value for a TextView. That is the font size that will fill it without being too big. The formula is:

fontSize = (TextView Height) * (width DPI / height DPI) / (Screen Width * Screen Height);

The width and height DPI can be acquired by Overriding the onConfigurationChanged method in your activity, and using newConfig.screenWidthDp and newConfig.screenHeightDp.

The screen size in pixels can be acquired using the following helper function

// Get Screen Size
static public Point getScreenSize(Activity aActivity)
{
    Display display = aActivity.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size;
}

I just calculate this value as needed (in onConfigurationChanged) and push it to some static helper class. Then when a TextView needs to determine what font size to use, it pulls it out of this class.

It works, but might not be the simplest solution.

0

Not that I am aware of, sorry.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253