Android layer-list example

In this Android tip, I am going give you an example using layer-list drawable object to customize a spinner to look as shown in the image below.



The layer-list drawable object is used to manage other drawable object. It draws the drawable objects in order. The last item of the layer-list draws on the top of other drawable objects.
To define a layer-list xml file, you start with the <layer-list> tag. The drawable objects to be managed by the layer-list must be placed in the <item> tags.
Now to have an example application on layer-list example, you create a new Android project. Then add a spinner to the activity_main.xml file. The value of its background attribute is spinner_bg_style that is a drawable object defined later in this tip.

<Spinner
     android:id= "@+id/spinner_outlet_type"
     android:layout_width="200dp"
     android:layout_height="60dp"
     android:padding="15dp"
     android:background="@drawable/spinner_bg_style"
     android:popupBackground="#cccccc"
     android:entries= "@array/array_outlettypes"
     />
In the res/drawable directory, you create an xml file called spinner_bg_style.xml that defines a layer-list. In the layer-list, there are two items: a shape object and a bitmap. The shape object defines the background of the spinner. The bitmap item represents the dropdown arrow of the spinner. The source of the bitmap is an image called arrow.png in the res/drawable directory. The bitmap draws on top of the shape.
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item><layer-list>
     <item>
        
       <shape>
           <gradient android:angle="90" android:endColor="#cccccc" android:startColor="#d8d8d8" android:type="linear" />
           <padding android:bottom="0dp" android:left="0dp" android:right="3dp" android:top="0dp" />
        </shape>



      </item>
     <item>
        <bitmap android:gravity="center|right" android:src="@drawable/arrow" />
     </item>
   </layer-list></item>

</selector>



Android-layer-list-example

Posted by: Dara | post date: 02-10-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.