How to Change the Background Color of Button in Android using ColorStateList?
Last Updated :
02 Jan, 2025
ColorStateList is an object which can define in an XML file that can be used to apply different colors on widgets (such as Buttons , etc) depending on the state of Widgets to which it is being applied . For Example, There are many states of Buttons like (pressed, focussed, or none of them ) and other widgets states like enable, checkable, checked, etc, Using Color State List is a nice way to change the color of the button without using shape drawables or custom images. One should remember the color state list can be used anywhere, where color is used. The color state list is defined in XML and saved under the res/color folder. The root element of the color state list is a selector and the item element is defined for each state that you want to define the color by using color and alpha attributes. The default color should be the last element which is used when color for a specific state is not defined. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.
Approach
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio . Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Go to the layout folder and in the activity_main.xml file change the ConstraintLayout to LinearLayout and give its orientation vertical. Add the Button and Switch to the layout. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<!--Linear Layout with the vertical orientation and center gravity-->
<LinearLayout
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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<!--Button whose background color depends on the
selector and state of the button-->
<Button
android:id="@+id/button"
android:layout_width="160dp"
android:layout_height="110dp"
android:backgroundTint="@color/button_background_color"
android:text="Click Me"
android:textColor="@color/button_text_color"
android:textSize="24sp"
android:textStyle="bold" />
<!--Switch with default state as enabled-->
<Switch
android:id="@+id/buttonSwitch"
android:layout_width="160dp"
android:layout_height="80dp"
android:checked="true"
android:text="Enabled"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
Step 3: Add a resource directory named as the color
Add a resource directory named as color to the res folder and keep the root element as a selector, since we want to select the color on the basis of the state. Add two resource files named as button_text_color.xml and button_background_color.xml to the color resource directory. Keep the selector as the root element for the same reason as mentioned above. For performing the above things refer to the below images and codes.
In order to create the color resource file, do right click on the res folder, click on New and select Android Resource Directory.

Now create both the resource file ( button_text_color.xml and button_background_color.xml ) within the color resource directory by doing right-click on the color directory and keeping the selector as the root element.

Below is the code for the button_background_color.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--When Button is not enabled -->
<item android:state_enabled="false" android:color="#b6b7b5" />
<!--When Button is in pressed state -->
<item android:state_pressed="true" android:color="#22a540" />
<!--When Button is in selected state -->
<item android:state_selected="true" android:color="#fabcff" />
<!--Default Background Color -->
<item android:color="@android:color/white" />
</selector>
Below is the code for the button_text_color.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--When the button is not enabled-->
<item android:state_enabled="false" android:color="@android:color/white" />
<!--When button is enabled-->
<item android:state_selected="true" android:color="@android:color/white" />
<!--Default Text Color-->
<item android:color="#db402c" />
</selector>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
// if button is already in selected state and now it is pressed
// again,then it will reach in not selected state and vice versa
button.isSelected != button.isSelected
}
buttonSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
// if the switch is checked,then enable the button,else not
button.isEnabled = isChecked
}
}
}
Output: Run on Emulator
Similar Reads
How to Change the Background Color After Clicking the Button in Android? In this article, we will see how we can change the background of the screen by clicking a button. For this, we will be using the onClick() method. When we click on the button the onClick function is called. To set the click handler event for the button we need to define the android:onClick attribute
3 min read
How to Change Colors of a Floating Action Button in Android? Android applications use the Floating Action Button for prompting the user to perform some important action within the android application. Floating Action Buttons in android applications are used to perform some important functionality within android applications. Many times in the android applicat
3 min read
How to Change Background Color of ListView Items in Android? In Android, a ListView is a layout element that is used to display items in a list. This is the simplest form of displaying a list or array of items and one can choose from pre-developed layouts for displaying an element without creating a separate layout unlike in other similar views. Each item in
2 min read
How to Change Background Image by Button Clicking Event in Android? Background Images play an important role in the beautification of any application. Hence, most social media applications like WhatsApp, Messenger provides this as a part of their feature to their users. So, keeping this in mind we will be going to develop an android application in which background i
3 min read
How to change the color of Action Bar in an Android App? Customizing the Action Bar allows you to enhance the visual appeal of your Android app. In this article, you will learn how to change the colour of the Action Bar in an Android App. Basically, there are two ways to change color.By changing styles.xml file:Just go to res/values/styles.xml fileedit th
2 min read
How to Change the Color of Status Bar in an Android App? A Status Bar in Android is an eye-catching part of the screen, all of the notification indications, battery life, time, connection strength, and plenty of things are shown here. An Android user may look at a status bar multiple times while using an Android application. It is a very essential part of
4 min read
How to Change TextField's Highlighted Text Color in Android using Jetpack Compose? In Android Jetpack Compose, Text is a UI element used to display characters, words, or sentences as strings on the screen. By default, this Text cannot be selected for copying. We have to create a SelectionContainer and then implement a Text inside it to make it selectable. When we try to select tex
3 min read
How to Change CardView Color When Selected in Android? CardView is a UI component in Android Studio that provides a simple way to display content with a raised or elevated appearance. It's part of the Android Support Library, which means it's compatible with Android devices running Android 5.0 (API level 21) or higher. A sample video is given below to g
2 min read
How to Change the Color of the Status Bar in Flutter Application? Flutter is an open-source UI toolkit developed and maintained by Google. It is used by big companies like Airbnb, Alibaba, and Google itself to serve billions of users around the globe. Status Bar is the topmost area of the screen which is home to the Battery Indicator, Network Status, Time, Notific
1 min read
Change Button Size in Android Jetpack Compose In Android, Button is a very common UI element that is used to call a function or perform a task when clicked by the user. A Button by default occupies specific space depending upon the text that it displays. If the Button text is longer or has a larger font size, the Button width, and height increa
3 min read