Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Android Color State List Resources

Tech 1

A ColorStateList is an XML-definable resource that acts as a dynamic color, changing based on the state of the Android View it’s applied to. For example, a button widget can be pressed, focused, or idle, and a color state list lets you assign distinct colors to each state.

These state-based colors are defined inside a single <selector> root elemant in an XML file. Each color uses an <item> tag, with attributes describing the conditions for its application. The system checks these items top-to-bottom whenever the view’s state changes, selecting the first item that meets all minimum matching state requirements (not the best overall match).


File Location & Resource Details

  • Storage Path: res/color/filename.xml (filename becomes the resource ID)
  • Compiled Type: Resource pointer to a ColorStateList instance
  • Resource References:
    • Java/Kotlin: R.color.filename
    • XML: @[package:]color/filename

XML Syntax

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:color="hex_color"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

Key Elements

  • <selector>: Root element. Contains one or more <item> tags.
    • Mandatory Attribute: xmlns:android (must be http://schemas.android.com/apk/res/android)
  • <item>: State-based color definition (child of <selector>).
    • Mandatory Attribute: android:color (hexadecimal color value starting with #, formats include RGB, ARGB, RRGGBB, AARRGGBB)
    • Optional State Attributes:
      • state_pressed: Applies when the view is pressed (e.g., button tapped)
      • state_focused: Applies when the view is focused (e.g., button highlighted via trackball)
      • state_selected: Applies when the view is selected (e.g., active tab)
      • state_checkable: Applies when the view can be checked/unchecked
      • state_checked: Applies when the view is checked (e.g., checked checkbox)
      • state_enabled: Applies when the view can receive input
      • state_window_focused: Applies when the app window has focus (foreground state)

Example

  1. Create the Color State List File: Save as res/color/btn_text_color.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:color="#ffff0000" /> <!-- Pressed state (red) -->
        <item android:state_focused="true" android:color="#ff0000ff" /> <!-- Focused state (blue) -->
        <item android:color="#ff000000" /> <!-- Default state (black) -->
    </selector>
    
  2. Apply to a View: Use @color/btn_text_color in your layout

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:textColor="@color/btn_text_color" />
    

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.