Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have layer-list inside selector

Tags:

android

I have the following drawables

<!-- button_shape.xml -->
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="3dp"/>
            <solid android:color="#006600" />
        </shape>
    </item>

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="3dp"/>
            <solid android:color="#003300" />
        </shape>
    </item>
</selector>

and

<!-- button_shape_shadowed.xml -->
<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="#D0D0D0"/>
        </shape>
    </item>
    <item>
        <inset
            android:drawable="@drawable/button_shape"
            android:insetBottom="5dp"
            android:insetLeft="5dp"
            android:insetRight="5dp"
            android:insetTop="5dp">
        </inset>
    </item>
</layer-list>

Is there a way to merge them into one? My main problem is that I can't have a selector inside inset. Since the button_shape.xml is not being used anywhere else, I don't want to create an extra file for no reason.

like image 987
Khanh Nguyen Avatar asked Jul 27 '14 04:07

Khanh Nguyen


1 Answers

Thanks guys! The following does work, even though Android Studio complains that selector is not allowed inside inset.

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="#D0D0D0"/>
        </shape>
    </item>
    <item>
        <inset android:insetBottom="2dp">
            <selector>
                <item android:state_pressed="false">
                    <shape android:shape="rectangle">
                        <corners android:radius="3dp"/>
                        <solid android:color="#006600" />
                    </shape>
                </item>

                <item android:state_pressed="true">
                    <shape android:shape="rectangle">
                        <corners android:radius="3dp"/>
                        <solid android:color="#003300" />
                    </shape>
                </item>
            </selector>
        </inset>
    </item>
</layer-list>
like image 91
Khanh Nguyen Avatar answered Oct 15 '22 22:10

Khanh Nguyen