34

I'm trying to have some margins inside a button between the text and the borders but I don't know how to specify them.

The only way I know is using android:layout_width="...", but this is not relative to the text size.

Thanks!

1
  • Where do you want the text to be, the text size has to be defined separately.
    – Dominic
    Dec 15, 2011 at 9:08

3 Answers 3

113

If you're looking for this kind of padding on the left and right of this cheeky text : enter image description here

Here is what you do. :

   <Button
    android:id="@+id/she_was_good"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:soundEffectsEnabled="false"
    android:layout_alignParentBottom="true"
    android:paddingRight="25dp"
    android:paddingLeft="25dp"
    android:text="@string/im_sorry_lol_str" />

The part you are looking for is paddingRight and paddingLeft

2
  • 1
    Probably would be best to replace with a non-photoshoped image. That image led me to think you were demonstrating how to have different width boarders on the left and right, which is clearly not the case. Jun 7, 2018 at 6:55
  • It was non-photo shopped but the text got kinda viral on reddit. Anyone who wants to do it please do. I have moved on from Android
    – Reno
    Oct 13, 2021 at 16:21
39

Use padding to add margin between border and text in button.

0
0

To measure its dimensions, a view takes into account its padding. The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.

Read more at the official doc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.