Articles → Android → Color State List Resources In Android

Color State List Resources In Android






What Is Color State List Resource?







How To Add A New File?




  1. Right-click on the color folder.
  2. Go to New → Color resource file.
  3. Picture showing the color resource file submenu in the new menu of the android studio
    Click to Enlarge

  4. Following window will appear. Enter the file name and click on Ok.
  5. Picture showing a popup to enter the new resource name
    Click to Enlarge


Adding Selector




<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!--pressed-->
    <item android:state_pressed="true" android:color="#FF0000"></item>
    <!-- default -->
    <item android:color="#228B22"/>
</selector>



Add Selector In Button




<Button    
                android:id="@+id/button11"    
                android:layout_width="match_parent"    
                android:layout_height="wrap_content"    
                android:text="Button" 
                android:textColor="@color/color_one" />




Picture showing changing the color of the button according to the state
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, October 31, 2019

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250