Thursday, March 12, 2020

android kotlin - Create ColorStateList programmatically

MainActivity.kt

package com.example.jetpack

import android.content.res.ColorStateList
import android.graphics.Color
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)

        // generate background tint list for button
        button2.backgroundTintList = generateColorStateList()

        // modify disabled color on method
        button3.backgroundTintList = generateColorStateList(
            // disabled color Alabaster
            disabledColor = Color.parseColor("#EDEAE0")
        )

        // background tint list for checkbox and radio button
        materialCheckBox1.buttonTintList = generateColorStateList()
        materialRadioButton.buttonTintList = generateColorStateList()
    }
}


// method to generate color state list programmatically
fun generateColorStateList(
    enabledColor:Int = Color.parseColor("#00BFFF"), // Capri
    disabledColor:Int = Color.parseColor("#A2A2D0"), // Blue bell
    checkedColor:Int = Color.parseColor("#7BB661"), // Bud green
    uncheckedColor:Int = Color.parseColor("#A3C1AD"), // Cambridge blue
    activeColor:Int = Color.parseColor("#1034A6"), // Egyptian blue
    inactiveColor:Int = Color.parseColor("#614051"), // Eggplant
    pressedColor:Int = Color.parseColor("#FFD300"), // Cyber yellow
    focusedColor:Int = Color.parseColor("#00FFFF") // Aqua
):ColorStateList{
    val states = arrayOf(
        intArrayOf(android.R.attr.state_enabled),
        intArrayOf(-android.R.attr.state_enabled),
        intArrayOf(android.R.attr.state_checked),
        intArrayOf(-android.R.attr.state_checked),
        intArrayOf(android.R.attr.state_active),
        intArrayOf(-android.R.attr.state_active),
        intArrayOf(android.R.attr.state_pressed),
        intArrayOf(android.R.attr.state_focused)
    )
    val colors = intArrayOf(
        enabledColor, // enabled
        disabledColor, // disabled
        checkedColor, // checked
        uncheckedColor, // unchecked
        activeColor, // active
        inactiveColor, // inactive
        pressedColor, // pressed
        focusedColor // focused
    )
    return ColorStateList(states, colors)
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Material Button Default"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Color States"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Disabled Button"
        android:enabled="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <com.google.android.material.checkbox.MaterialCheckBox
        android:id="@+id/materialCheckBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:text="Check Me"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3" />

    <com.google.android.material.radiobutton.MaterialRadioButton
        android:id="@+id/materialRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:text="Select Me"
        app:layout_constraintBottom_toBottomOf="@+id/materialCheckBox1"
        app:layout_constraintStart_toEndOf="@+id/materialCheckBox1" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples