3
votes

I have an android application in which a user can create what is basically a macro and label that macro with some text. I then create a button for them with their descriptive text. The button is a custom view extending Button. In the constructor I set the layout as follows:

this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT));

These buttons are then placed within a GridView. Functionally, it's working as intended but I'm running into a layout issue.

If the text is too long, it will break and wrap to the next line, thus increasing the height of the button while maintaining a constant width. The problem is in how the text wraps, it will break in the middle of a word, instead of gracefully wrapping at whitespace. For instance the test "Perform an Action" will render as

Perform an Ac
    tion

Ideally, I'd like to wrap gracefully at whitespace instead of breaking words across lines. I suppose I could do this by checking the length of the text and the font against the width of the button and then doing some fancy insertion of newlines myself, but that gives me traumatic flashbacks to making win32 UIs. Is there a better way?

3

3 Answers

2
votes

you can add this attribute to your Button's XML that will magically put the whole text in one line:

android:singleLine="true"

or you can verify the text before you insert it to the button and check the number of words.. if it is too long like more than 25 characters then break it on the second or third whitespace then set it to the button.

hope I got your question right.

1
votes

AFAIK there's no simple way to do precisely what you want. You can get a decent look using android:singleLine="true" and android:ellipsize="marquee". Also, since you have already implemented your own Button class, take a peek at this question

0
votes

I had a button which said "Off" and had a drawable to the left, but it was wrapping to two lines. i.e.

Image O
      ff

The solution was that I removed the drawablePadding style. i.e.

<item name="android:drawablePadding">5dp</item>