I created a button with a 9patch image as a background. In the 9patch image I used the bottom and right lines to specify the content area/padding, to position the text on the button. This works well on a TextView, but on a Button the content lines seem to have no effect on the text position. Setting “paddingTop”/”paddingBottom” also seem to have no effect – only “padding” has any effect.
This is my layout XML:
<Button
android:layout_width="450dp"
android:layout_height="198dp"
android:text="Some text here"
android:gravity="center"
android:background="@drawable/my_button"/>
Is there any way to set the padding? Maybe I could use a different View instead of a Button? For example, I could try to just use a TextView and make it clickable, but I’m not sure what other side-effects this will have.
Explicitly setting
android:padding="@null"solved my issue.The issue seemed to be that a Button (or one of the styles in the theme I’m using) is setting the padding, which overrides all other padding in the button – padding in the background drawable, as well as
paddingTop,paddingLeftetc.My button layout is now defined like this:
My IDE (IntelliJ IDEA 11.1) picked up the
@nullas an error, even though it compiled. Setting it to-1pxalso worked.