Micah Hainline
Micah Hainline

Reputation: 14437

How can I make button text extend outside 9 patch content area?

I have a row of buttons with custom 9 patch images, and variable length text. I would like the buttons to be the same height. When the text is long enough to wrap, it expands the button size, making the button with wrapped text bigger than the others. I'm laying these buttons out in code in linear layouts. I can fix the size of the button, but then it just cuts off the bottom. How can I make the text take up more of the padding space of the button, so that the text butts up against the top line of the button?

Upvotes: 0

Views: 1069

Answers (2)

Micah Hainline
Micah Hainline

Reputation: 14437

Ultimately I was unable to find a nice way of doing this without creating a custom view class.

Upvotes: 0

bigstones
bigstones

Reputation: 15267

9patch content area is just used to set a padding. If you change the button's padding you will override the one set by the 9patch.

edit

try something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button android:text="ButtonButton"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingTop="0dp"
        android:paddingBottom="10dp"/>
    <Button android:layout_width="wrap_content" android:text="Button"
        android:layout_height="match_parent"/>
    <Button android:layout_width="wrap_content" android:text="Button"
        android:layout_height="match_parent"/>
</LinearLayout>

this way the button will be as high as the others. Sadly, I don't know if it depends on the original button's 9patch that might have asymmetrical paddings, but I can't make the text align with that from other buttons (I think gravity is by default set to center). Maybe with your 9patch it works though. (EDIT: oh, but if you'll have two lines of text who cares about alignment)

Upvotes: 1

Related Questions