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

  • How to deal with missing src/test/java source folder in Android/Maven project?
  • Unable to remove an in-app subscription from my google play developer console
  • Using Dependency Injection with Roboguice?
  • Hide soft keyboard on losing focus
  • How to ignore complete folders for lint checking with gradle?
  • Android back button behaviour
  • <?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?

    Related posts:

    Unzip a zipped file on sd card in Android application
    How do I use sharedPreferences outside of an Activity?
    Bind ButterKnife to Dialog fails
    Is there an easy way to add a border to the top and bottom of an Android View?
    Divide elements on groups in RecyclerView
    Implementing a File Picker in Android and copying the selected file to another location
  • Replace one Fragment with another in ViewPager
  • Android Device not recognized by adb
  • Gradle version 2.2 is required. Current version is 2.10
  • AsyncTask Android example
  • How i will detect motion sensor in broadcast receiver?
  • how to set google map fragment inside scroll view
  • 2 Solutions collect form web for “Change Padding in Selector drawable”

    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" />
    

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

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.