8

Is there a way to know how many characters of font size 10sp can fit into a TextView of a fixed width (let's say 100dp)?

I need to track if the whole string will fit (be visible) into a TextView of predefined width.

I haven't been able to find a way to track or calculate this.

sandalone
  • 41,141
  • 63
  • 222
  • 338

2 Answers2

10

Since Android uses proportional fonts, each character occupies a different width. Also if you take kerning into account the width of a string may be shorter than the sum of the widths of individual characters.

So it's easiest to measure the whole string by adding one character at a time until (a) the entire string if found to fit within the limit, or (b) the width of the string exceeds the limit.

The following code shows how to find how many characters of size 11px fits into TextView 100px wide. (You can find formulas to convert between px & dp on the Web).

We'll do it by using measureText(String text, int start, int end) in a loop incrementing the value of end until it it no longer fits the TextView's width.

String text = "This is my string";
int textViewWidth = 100;
int numChars;

Paint paint = textView.getPaint();
for (numChars = 1; numChars <= text.length; ++numChars) {
    if (paint.measureText(text, 0, numChars) > textViewWidth) {
        break;
    }
}

Log.d("tag", "Number of characters that fit = " + (numChars - 1));

If performance is poor, you may try using a binary search method instead of a linear loop.

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
  • How can I know how many characters of size 11dp (if needed, I may use px or pt values) fits into TextView 100dp wide? – sandalone Jul 05 '12 at 16:49
0

The best way to determine this would just be to test it. Since you are using DP it will be relatively device-independent. Unless you are using a fixed-width font there isn't really a way to determine it theoretically unless you want to actually try and measure the width of each letter, the kerning, etc. and compare to the width of the TextView

matt5784
  • 3,065
  • 2
  • 24
  • 42
  • Cannot test. The text is delivered via feed and it displays it. Sometimes it has 20 characters, sometimes 120. I need to detect overflowing length so that I can decrease font size. – sandalone Jul 05 '12 at 16:47
  • Shouldn't it wrap automatically? – matt5784 Jul 05 '12 at 16:49
  • Not if I set that only 4 lines are visible. The text that does not fit will not be visible in string is longer than 4 lines of 100dp width. – sandalone Jul 05 '12 at 16:50
  • Okay, so assume for a moment you can check and decrease the text size so it fits. What happens if you get 500 characters? Your text will get so small it won't be able to be read. A better solution might be to simply only show the first X number of characters and then an ellipsis (...) to indicate there is additional text which isn't being shown. – matt5784 Jul 05 '12 at 16:52
  • it will never take so much characters. at the max characters, it usually lacks around 5 characters. and it's impossible to omit those 5. the current version simply does not show them, but I thought there is a way to show them all – sandalone Jul 06 '12 at 12:32