Android Using a list of layers to select buttons

How we use the list of layers as a template for the button. I have a button:

<item android:state_pressed="true"> <shape> <gradient android:endColor="@color/white" android:startColor="@color/grey_blue_light" android:angle="90" /> <stroke android:width="1dp" android:color="@color/aqua_blue" /> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item android:state_focused="true"> </item> <item> </item> 

Now I need a list of layers that will be used as a form when I click the button's status button:

 <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <solid android:color="@color/aqua_blue" /> </shape> </item> <item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp"> <shape android:shape="oval"> <solid android:color="@color/aqua_blue" /> </shape> </item> 

How do we use this list of layers in the button selector?

+42
android button customization
Dec 01 '11 at 10:07
source share
2 answers

Step 1 Create three different layer_list xml in a drawable folder for three different button states. For example, the name of these xml is layer1.xml, layer2.xml, layer3.xml

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="270" android:startColor="#0000ff" android:endColor="#0000dd" android:type="linear" /> </shape> </item> </layer-list> 

Step 2 create an xml selector named btn_background.xml and pass the layer_list xml attribute in the drawable attribute

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/layer1"> </item> <item android:state_focused="true" android:drawable="@drawable/layer2"> </item> <item android:drawable="@drawable/layer3"> </item> </selector> 

step 3 Set the xml selector as the background of the android:background="@drawable/btn_background" button android:background="@drawable/btn_background"

+113
Dec 01 '11 at 10:30
source share

Just replace the shape tag with layer-list and everything will work fine.

0
Jan 05 '17 at 13:18
source share



All Articles