11

I don't believe this exists, but wanted to double-check. I'd like to set a TextView's text size such that it would fit within a given width, single line. Example:

<LinearLayout
  layout_width="100dip"
  layout_height="50dip">

  <TextView
    layout_width="fill_parent"
    layout_height="wrap_content"
    textSize="fill" 
    text="fit me please!" />

</LinearLayout>

Thanks

Ron
  • 24,175
  • 8
  • 56
  • 97
user291701
  • 38,411
  • 72
  • 187
  • 285

4 Answers4

11

You can use the TextUtils.EllipsizeCallback. When the text gets ellipsized this callback is done by the textview. Here you can set text size smaller than the current.

EDIT : Otherwise you can use TextUtils.ellipsize this way

while (mText != TextUtils.ellipsize(mText, textPaint, other params)) { 
    textpaint.setTextSize(textpaint.getTextSize() - 1);
}
Ron
  • 24,175
  • 8
  • 56
  • 97
  • 1
    Be careful.. This is the right idea, but getTextSize() returns PX and setTextSize() takes DIP. You'll want to not feed one into the other. Keep track of the size in an independent variable.. – Mason Lee Sep 03 '11 at 00:27
  • 1
    I am setting the textsize to the paint object not to the textview. When I find the right size, only then I set it to the textview. So I see no problem in using this idea. – Ron Sep 03 '11 at 07:03
  • 1
    @userSevens7s Oh, carry on! I hadn't read that right the first time and stumped myself on the stupid gotcha I mentioned. Cheers. – Mason Lee Sep 04 '11 at 08:55
  • @userSeven7s, I'm using this approach and I'm getting `java.lang.StackOverflowError` in this devices: GT-I9192, D5803, with API 4.2.2, 4.4.4 – GuilhE Jul 03 '15 at 15:15
5

This custom function should do it, using TextUtils.ellipsize ...

public static void shrinkTextToFit(float availableWidth, TextView textView,
                                   float startingTextSize, float minimumTextSize) {

    CharSequence text = textView.getText();
    float textSize = startingTextSize;
    textView.setTextSize(startingTextSize);
    while (text != (TextUtils.ellipsize(text, textView.getPaint(),
                                        availableWidth, TextUtils.TruncateAt.END))) {
        textSize -= 1;
        if (textSize < minimumTextSize) {
            break;
        } else {
            textView.setTextSize(textSize);
        }
    }
}
Mason Lee
  • 5,130
  • 1
  • 20
  • 15
  • I'm using this approach and I'm getting `java.lang.StackOverflowError` in this devices: GT-I9192, D5803, with API 4.2.2, 4.4.4 – GuilhE Jul 03 '15 at 15:13
0

Here is a good packaged solution to auto-fit text: https://github.com/grantland/android-autofittextview

Mason Lee
  • 5,130
  • 1
  • 20
  • 15
0

You can always combine the xml layout definition with java and dynamically define the size of the text. You will probably need some calculations and references from the dip units to the pixel density information about the current screen in use, but once done, it should almost guaranteed solve your problem for all cases.

jasoares
  • 1,811
  • 17
  • 22
  • Hi yeah I've written something to do this (it's a bit ugly) was hoping a newer version of the SDK may have added a new flag to do the same thing. – user291701 Aug 31 '11 at 15:10