Android getTextBounds trailing whitespace handling (also relevant for AndEngine Font.getStringWidth)

android, fonts, whitespace

Solution

It seems to be a feature, not a bug, see the discussion at http://code.google.com/p/android/issues/detail?id=7527 , since the bound is the visual bound.

Workaround: add an extra `x` character to the text, measure it, and subtract the width of the `x`.

Problem

When doing `mPaint.getTextBounds()` or AndEngine's `Font.getStringWidth`, the trailing space if not taken into account.

Original source