Android selector xml

In this Android tip, I am going to show you how to use Selector to change text colors and shapes of buttons when they are in default state and when they are pressed. You can describe the state list in an XML file. Each color or shape is defined in an <item> element inside a single <selector> element. Each <item> have different attributes to describe the state in which it should be used. In the example below, for the first button, the Selector is used to change the colors of the button, depending on the state of the button. For the second button, the Selector changes the shapes of the button when its state changes.


Now to have an example application on Button Selector, you create a new Android project. Then add two buttons to the activity_main.xml file.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  
   tools:context="com.example.andexample.MainActivity" >

   <Button
     android:id="@+id/bt1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Button"
     android:textColor="@color/button_color" />
     />
     <Button
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Button"
     android:background="@drawable/button_shape"
     android:layout_below="@+id/bt1"
      />
     />
    
    

</RelativeLayout>


Then in the res/color folder, create button_color.xml file that has the following content. The button_color.xml file is applied to the first button to provide a different color during each state (default and pressed states)

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

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true"
       android:color="#44aa77"/> <!-- pressed -->
   <item android:color="#000000"/> <!-- default -->
</selector>


In the res/drawable folder, you create button_shape.xml file that will be applied to the second button to provide a different shape during each state.

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

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true" > <!-- pressed state -->
     <shape>
        <solid
           android:color="#0000ff" />
        <stroke
           android:width="1dp"
           android:color="#0000ff" />
        <corners
           android:radius="0dp" />
        <padding
           android:left="10dp"
           android:top="10dp"
           android:right="10dp"
           android:bottom="10dp" />
     </shape>
   </item>
   <item
>
<!-- default -->
    
<shape>
        <gradient
           android:startColor="#009900"
           android:endColor="#009900"
           android:angle="270" />
        <stroke
           android:width="1dp"
           android:color="#171717" />
        <corners
           android:radius="0dp" />
        <padding
           android:left="10dp"
           android:top="10dp"
           android:right="10dp"
           android:bottom="10dp" />
     </shape>

   </item>
</selector>

Android-selector-xml

Posted by: Dara | post date: 11-11-2014 | 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.