Change Margin of a Layout using XML in Layer-List

princepiero picture princepiero · Aug 29, 2013 · Viewed 19k times · Source

I tried several options but I can't find the best way to do this: I want to change the margin of a Linear Layout when button is clicked using XML. Here's my code for the selected button:

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

    <item android:bottom="-1dp">
        <shape android:shape="rectangle" >
            <corners
                android:topLeftRadius="5dp"
                android:topRightRadius="5dp" />

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

            <stroke
                android:width="1dp"
                android:color="#2d2e2f" />
        </shape>
    </item>
</layer-list>

Now I want to add the changing of margin which if put in a separate file would look like this:

<resources>
    <style android:id="@+id/tab_label" >
        <item android:layout_marginTop="7dp"/>
    </style>
</resources>

I tried adding it as an item to the layer list but it returns an error.

Putting it in another file is quite problematic because Android can't allow two styles to be used in one element. So I want to merge the two things in one XML file.

Or, what's the best way doing this?

Answer

princepiero picture princepiero · Sep 7, 2013

Just solved it after roughly a week of trials. On this line of code:

<item android:bottom="-1dp">

Modify it to this:

<item android:bottom="-1dp" android:top="4dp">

You can customize the the number based on your preference.