Как менять фон на kotlin
Для установки фонового цвета применяется модификатор background() . Есть две версии этой функции. Первая версия:
color : значение цвета в виде объекта Color
shape : закрашиваемая форма — объект Shape . По умолчанию это прямоугольная форма
brush : применяемая для покрытия цветом кисть в виде объекта Brush
shape : закрашиваемая форма — объект Shape . По умолчанию это прямоугольная форма
alpha : значение прозрачности. Оно должно находится в диапазоне от 0 (полностью прозрачно) до 1 (отсутствие прозрачности)
В данном случае разберем только установку цвета.
Color. Определение цвета
В Jetpack Compose цвет представлен классом Color из пакета androidx.compose.ui.graphics. .
Есть несколько способов создания цвета. Рассмотрим некоторые:
Color(value) : в конструктор передается шестнадцатеричное значение цвета. Например
Color(red, green, blue, alpha) : в конструктор передается числовые значения для каждой отдельной компоненты цвета.
Dynamically change Button background in Kotlin Android
In this Android Tutorial, we shall learn to dynamically change button background in Kotlin Android.
There could be scenarios where you might need to change the background of a button to show a state of the application or status of an update or status of a player, etc.
setBackgroundResource() method is used to change the button background programmatically. setBackgroundResource(int id) accepts id of drawable resource and applies the background to the button.
Useful links To set a drawable background to button : Custom design for Button background To set a onClickListener to button : Button setOnClickListener
Example – Dynamically Change Button Background in Kotlin Android
In the following Kotlin Android Example, we shall create a button in layout xml and dynamically change its background on the button click using Button OnClickListener.
layout xml file : activity_change_button_background.xml
Kotlin Activity file : ChangeButtonBackground.kt
/app/src/main/res/drawable/btn_center_gradient.xml
/app/src/main/res/drawable/btn_oval_gradient.xml
Following are the screenshots of the application.
Click on the button to change the background.
Button with initial backgroud

Button background changed on button click
Как менять фон на kotlin
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.