Android change button colors

How to Change the Background Color of Button in Android using ColorStateList?

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.

Читайте также:  Как закачать вацап для андроид

Источник

Change colour of button in Android Studio

As you know, the Default color of the button in the android studio is grey which is not suits for UI. So, here we are going to discuss how to change the colour of the button in android studio using different methods

Table of Contents

Method 1: Using XML

First, add the button in XML layout like this

Normal color of button

After this you will see your button looks something like this

Now you need to add another tag that is android:background=”color Hexa code” like this,

Now, you see your button looks like this

coloured color of button

Method 2: Using a drawable file

This is my best method because it provides you many other operations to perform on the button like rounded corner etc.

Right click on drawable -> new-> drawable resource file and named as background.

Add following line of code there

You see something like this

drawable file

Now, goto your button and drawable like this

coloured button

We update this content as we see more and more methods to change the colour of button in android studio.

How to add button in android studio?

Simply add from the design section of XML layout.

How to change the colour of button in android studio?

Add android:background=”colour” in button

Источник

Оцените статью