19

I'm trying to padd my button background image of my main menu (I'm using a selector for the different states), doing it on this way (buttoninicio_custom.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/botoninicial_pressed"
          android:state_pressed="true">

            <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />

    </item>
    <item android:drawable="@drawable/botoninicial_pressed"
          android:state_focused="true">

            <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />

    </item>
    <item android:drawable="@drawable/botoninicial">
            <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </item>
</selector>

..but the padding has no effect. What I should do to solve this problem??

I already used "bitmap" tag inside each "item" tag with the padding inside, but it's still doing anything!!!

My main button looks this way:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button2"
        android:layout_width="180dp"
        android:layout_height="50dp"
        android:layout_marginBottom="15dp"
        android:textSize="16sp"
        android:textColor="#FFFFFF"
        android:background="@drawable/buttoninicio_custom"
        android:text="@string/idmMENU2" />
</LinearLayout>

"Because if I set android:padding inside the "button" tag, it pads me the text not the background image... The problem it's that when I pressed the main button: My image background change correctly but the new image appear cut."

user1592470
  • 401
  • 2
  • 8
  • 21
  • 1
    it seems you are not changing the padding, its constant.. so why you are not using it in Button attribute instead of selector, android:padding="10dp" – Ishtiaq Sep 03 '12 at 12:29
  • Because if I set android:padding inside the "button" tag, it pads me the text not the background image... The problem it's that when I pressed the main button: My image background change correctly but the new image appear cut. – user1592470 Sep 03 '12 at 12:31
  • i don't think so you can set padding to background, don't you want to use image button ?? – Ishtiaq Sep 03 '12 at 12:36
  • that's my problem: http://postimage.org/image/rh0amfccv/ – user1592470 Sep 03 '12 at 13:01
  • if,its not working like this.. so put the button in Another layout and set the padding of the layout and background to your button if you don't want to use image button – Ishtiaq Sep 04 '12 at 04:27

2 Answers2

68

Wrap it in a layer-list:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="1dp">

        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- your original selector content -->
        </selector>
    </item>
</layer-list>
Oliv
  • 10,221
  • 3
  • 55
  • 76
  • adding to just the `item` tag did not work instead, it had to be wrapped as stated...thank you – CrandellWS Apr 30 '16 at 08:26
  • This works great! You literally just add the layer-list wrapping shown above to your selector.xml file, and it just works. – bstar55 Jun 21 '16 at 20:41
10

padding attribute is of no use under the tag item. If you press ctrl+space(autocomplete) then android won't suggest using this tag though android won't give any error. You can define padding when you are creating a shape drawable.Something like this:

<item android:state_pressed="true">
    <shape >
        <solid
            android:color="#2c68e7" />
        <stroke 
            android:width="1dp"
            android:color="#2c68e7" />
        <corners
            android:radius="4dp" />
        <padding
            android:left="10dp"
            android:right="10dp"
            android:top="10dp"
            android:bottom="10dp" />
    </shape>
</item> 

But you cannot use an image resource in the shape drawable.

The better solution would be to use images of same size and dimension for the view.

karn
  • 5,963
  • 3
  • 22
  • 29
  • I used the same size image's and the problem still follow there – user1592470 Sep 03 '12 at 12:58
  • only size doesn't matter ...the image resolution and where have you placed the image also matter. If your device is mdpi and u'r image is in hdpi then it will be scaled down. – karn Sep 03 '12 at 16:33