Android Button Selector Color

Written by: Arvind Rai,
Last updated:
September 27, 2015
This page will provide Android button Selector color example. Here we will change button text color for different states. In our selector we are providing a default color and for state focused, there will be different color and for state pressed, the text will show different color.

<selector> Tag

Selector tag facilitates to allow different background or different color for different states. In our example we are using below selector.
xml/color_selector.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="#4CAF50"/> 
    <item android:state_focused="true"
          android:color="#9C27B0"/> 
    <item android:color="#F44336"/> 
</selector> 
On different button event, different color will be shown. We are applying this selector for button text color.
android:textColor="@xml/color_selector"
 

Code for Activity


MainActivity.java
package com.concretepage;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
    private final static String TAG = "MainActivity"; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
	   setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
           getMenuInflater().inflate(R.menu.menu_main, menu);
           return true;
    }
} 

Layout XML


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:ads="http://schemas.android.com/apk/res-auto" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true">  	  
		<LinearLayout
		    android:id="@+id/myLayout"
		    android:orientation="vertical"
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    android:background="#FFFFFF"
		    android:fillViewport="true">
		    <Button
		        android:id="@+id/button"
		        android:layout_width="fill_parent"
		        android:layout_height="wrap_content"
		        android:text="@string/btn"
		        android:textColor="@xml/color_selector"/>
		</LinearLayout>
</ScrollView> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.concretepage"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="14"/>
    <application 
        android:allowBackup ="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity  android:name="com.concretepage.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

Output

1. On pressed.
Android Button Selector Color Example
2. On focused.
Android Button Selector Color Example

Download Source Code

Join the Newsletter

(Subscribe to get our latest content directly into your inbox)

WRITTEN BY
ARVIND RAI
ARVIND RAI









©2024 concretepage.com | Privacy Policy | Contact Us