Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Padding in Selector drawable

i am trying to change the padding from a Button in a custom drawable ressource, and reuse this in a selector. Changing the drawable seems not the Problem, but the App always use the highest padding aviable from the different drawables:

button_inactive.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape android:shape="rectangle" >
        <solid android:color="#FF4d681f" />
    </shape>
</item>
<item
    android:bottom="2dp"
    android:left="2dp"
    android:right="2dp"
    android:top="2dp">
    <shape android:shape="rectangle" >
        <solid android:color="#FFFFFFFF" />

        <padding
            android:bottom="5dp"
            android:top="5dp" />
    </shape>
</item>

button_active.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle" >
        <solid android:color="#FF4D681F" />
    </shape>
</item>
<item
    android:bottom="2dp"
    android:left="2dp"
    android:right="2dp"
    android:top="2dp">
    <shape android:shape="rectangle" >
        <solid android:color="#FF8da32d" />

        <padding
            android:bottom="10dp"
            android:top="10dp" />
    </shape>
</item>

button_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/button_active"/>
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/button_active"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/button_active"/>
<item android:drawable="@drawable/button_inactive"/>

The Problem is that my Button uses the 10dp padding in all states, including the "inactive" state.

Is there a Solution to get it working only with XML or it is better to make a custom Button programaticly?

like image 695
IFR Avatar asked May 02 '13 08:05

IFR


People also ask

What is drawable padding?

android:drawablePadding is used for space between image and text.

What is layerlist in android?

Layer list. A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top. Each drawable is represented by an <item> element inside a single <layer-list> element.

What is padding in XML?

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 number 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.


2 Answers

try this

<solid android:color="#ffffff" />

<stroke
    android:width="2dp"
    android:color="@android:color/black" />

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

<corners
    android:bottomLeftRadius="6dp"
    android:bottomRightRadius="6dp"
    android:topLeftRadius="6dp"
    android:topRightRadius="6dp" />

like image 132
Omar Hassan Avatar answered Nov 11 '22 03:11

Omar Hassan


Add android:variablePadding="true" attribute to your selector.

I'm not sure how (and if) is this working when you are using layer-list, however I decided to share it anyway for anyone looking for the answer (as I was).

like image 5
sidon Avatar answered Nov 11 '22 03:11

sidon