Android Question Determine if text will not fit in a fixed size label

Jimdriver2

Member
Licensed User
Longtime User
I've got a label on a layout that can't grow. (It takes into account dip etc so it will be different sizes for different devices but there isn't the flexibility for it to become vertically larger)

I've then got a large paragraph of text that potentially will be too big to all be displayed in the label. What I want to do is trim that text and alter it to indicate to the user there is more to read. (On a click of the label I'll show it in a dialog.) So the label should be filled fully but the text won't just disappear.

Are there any libraries or functions I can use to determine if an string will fit entirely inside a label?

Thanks.
 

Mahares

Expert
Licensed User
Longtime User
Upvote 0

Jimdriver2

Member
Licensed User
Longtime User
Thanks for the replies. I've looked at those however and I'm not sure they are the answer.

The measureText routine is just for single lines. The AutoTextSizeLabel is going to alter the size of my text (and possibly also only handles one line) which I don't want.

I'm trying to just find out a way that tells me if some of the text in the label is not visible because it can't fit in the label. I want to then alter that text, taking off all the text that can't fit in and replace it with ... so that it indicates to the user there is more text to read.

I suppose a scrollview would be an alternative to avoid text being cut off, but I'm not sure I want that.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
The MeasureText and StringUtils.MeasureMultilineText methods are the answer, you will need to write a routine that can give you a max number of characters based on label width/height in the current font and point size. Once you have that you can trim the text to fit the space.

The only other alternative would be to set the label to single-line and make it MARQUEE style.
 
Upvote 0
Top