MainActivity.kt
package com.cfsuman.kotlintutorials
import android.content.res.ColorStateList
import android.graphics.BlendMode
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Build
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the widgets reference from XML layout
val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)
val radioAndroid = findViewById<RadioButton>(R.id.radioAndroid)
val radioKotlin = findViewById<RadioButton>(R.id.radioKotlin)
val radioFireworks = findViewById<RadioButton>(R.id.radioFireworks)
val radioColdFusion = findViewById<RadioButton>(R.id.radioColdFusion)
val tvResult = findViewById<TextView>(R.id.tvResult)
// change the radio buttons button color
radioAndroid.setCircleColor(Color.parseColor("#1974D2"))
radioKotlin.setCircleColor(Color.parseColor("#C32148"))
radioFireworks.setCircleColor(Color.parseColor("#696969"))
radioColdFusion.setCircleColor(Color.parseColor("#FFBA00"))
// radio group checked change listener
radioGroup.setOnCheckedChangeListener { radioGroup, i ->
// get the radio group checked radio button
findViewById<RadioButton>(i)?.apply {
// show the checked radio button's text in text view
tvResult.text = text
}
}
}
}
// extension function to change radio button circle color programmatically
fun RadioButton.setCircleColor(color: Int){
val colorStateList = ColorStateList(
arrayOf(
intArrayOf(-android.R.attr.state_checked), // unchecked
intArrayOf(android.R.attr.state_checked) // checked
), intArrayOf(
Color.GRAY, // unchecked color
color // checked color
)
)
// finally, set the radio button's button tint list
buttonTintList = colorStateList
// optionally set the button tint mode or tint blend mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
buttonTintBlendMode = BlendMode.SRC_IN
}else{
buttonTintMode = PorterDuff.Mode.SRC_IN
}
invalidate() //could not be necessary
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#E5E4E2"
android:padding="24dp">
<TextView
android:id="@+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:padding="8dp"
android:text="Which is your most favorite?"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvTitle">
<RadioButton
android:id="@+id/radioAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<RadioButton
android:id="@+id/radioKotlin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin" />
<RadioButton
android:id="@+id/radioFireworks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fireworks" />
<RadioButton
android:id="@+id/radioColdFusion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ColdFusion" />
<RadioButton
android:id="@+id/radioFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flash" />
</RadioGroup>
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="#32174D"
android:textSize="30sp"
android:fontFamily="sans-serif"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - Get app first install time
- android kotlin - Detect screen orientation change
- android kotlin - Get screen orientation programmatically
- android kotlin - Convert dp to pixels programmatically
- android kotlin - Get screen size from context
- android kotlin - RecyclerView horizontal
- android kotlin - RecyclerView divider line
- android kotlin - GridView item height
- android kotlin - GridView add item dynamically
- android kotlin - CheckBox checked change listener
- android kotlin - Set CardView elevation
- android kotlin - Change checked RadioButton text color
- android kotlin - RadioGroup onCheckedChange listener
- android kotlin - Get RadioGroup selected item
- android kotlin - RadioButton onClick event