Combine StateListDrawable and LevelListDrawable

Is there a way to combine the effects of StateListDrawable and LevelListDrawable?

I wrote this resource file, but when using nothing is displayed:

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
        <selector
                android:constantSize="true"
                android:variablePadding="false"
                android:maxLevel="0"
                >
                <item android:state_pressed="true">
                        <layer-list>
                                <item android:drawable="@drawable/btu_color_standard" />
                                <item android:drawable="@drawable/btu_shade_pressed" />
                        </layer-list>
                </item>
                <item android:state_focused="true">
                        <layer-list>
                                <item android:drawable="@drawable/btu_color_standard" />
                                <item android:drawable="@drawable/btu_shade_selected" />
                        </layer-list>
                </item>
                <item>
                        <layer-list>
                                <item android:drawable="@drawable/btu_color_standard" />
                                <item android:drawable="@drawable/btu_shade_normal" />
                        </layer-list>
                </item>
        </selector>
        <selector
                android:constantSize="true"
                android:variablePadding="false"
                android:maxLevel="1"
                >
                <item android:state_pressed="true">
                        <layer-list>
                                <item android:drawable="@drawable/btu_color_standard" />
                                <item android:drawable="@drawable/btu_lighted" />
                                <item android:drawable="@drawable/btu_shade_pressed" />
                        </layer-list>
                </item>
                <item android:state_focused="true">
                        <layer-list>
                                <item android:drawable="@drawable/btu_color_standard" />
                                <item android:drawable="@drawable/btu_lighted" />
                                <item android:drawable="@drawable/btu_shade_selected" />
                        </layer-list>
                </item>
                <item>
                        <layer-list>
                                <item android:drawable="@drawable/btu_color_standard" />
                                <item android:drawable="@drawable/btu_lighted" />
                                <item android:drawable="@drawable/btu_shade_normal" />
                        </layer-list>
                </item>
        </selector>
</level-list>

Then i call

_btn.getBackground().setLevel(level); //level is either 0 or 1

to change the drawable level (_btn - btw button).

Why is this not working?

Thanks in advance.

+3
source share
2 answers

Found a solution: <selector> elements should be enclosed in <item> elements:

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
                android:maxLevel="0"
                >
                <selector
                        android:constantSize="true"
                        android:variablePadding="false"
                        >
                        <item android:state_pressed="true">
                                <layer-list>
                                        <item android:drawable="@drawable/btu_color_standard" />
                                        <item android:drawable="@drawable/btu_shade_pressed" />
                                </layer-list>
                        </item>
                        <item android:state_focused="true">
                                <layer-list>
                                        <item android:drawable="@drawable/btu_color_standard" />
                                        <item android:drawable="@drawable/btu_shade_selected" />
                                </layer-list>
                        </item>
                        <item>
                                <layer-list>
                                        <item android:drawable="@drawable/btu_color_standard" />
                                        <item android:drawable="@drawable/btu_shade_normal" />
                                </layer-list>
                        </item>
                </selector>
        </item>
        <item
                android:maxLevel="1"
                >
                <selector
                        android:constantSize="true"
                        android:variablePadding="false"
                        >
                        <item android:state_pressed="true">
                                <layer-list>
                                        <item android:drawable="@drawable/btu_color_standard" />
                                        <item android:drawable="@drawable/btu_lighted" />
                                        <item android:drawable="@drawable/btu_shade_pressed" />
                                </layer-list>
                        </item>
                        <item android:state_focused="true">
                                <layer-list>
                                        <item android:drawable="@drawable/btu_color_standard" />
                                        <item android:drawable="@drawable/btu_lighted" />
                                        <item android:drawable="@drawable/btu_shade_selected" />
                                </layer-list>
                        </item>
                        <item>
                                <layer-list>
                                        <item android:drawable="@drawable/btu_color_standard" />
                                        <item android:drawable="@drawable/btu_lighted" />
                                        <item android:drawable="@drawable/btu_shade_normal" />
                                </layer-list>
                        </item>
                </selector>
        </item>
</level-list>
+8
source

In my case, the problem was that I did not set the button to use the background in the list of levels. For example, if you have a list of levels defined in res / drawable / button_levels.xml, you need to set the button background as "@ drawable / button_levels".

0

All Articles