MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import com.google.android.material.button.MaterialButton
import com.google.android.material.button.MaterialButtonToggleGroup
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val materialButtonToggleGroup = findViewById<MaterialButtonToggleGroup>(
R.id.materialButtonToggleGroup)
val textView = findViewById<TextView>(R.id.textView)
materialButtonToggleGroup.addOnButtonCheckedListener {
group, checkedId, isChecked ->
val listenerButton:MaterialButton = group.findViewById(checkedId)
val checkedButton:MaterialButton? = group
.findViewById(group.checkedButtonId)
if (checkedButton!=null){
textView.text = "Checked : ${checkedButton.text}"
if (!isChecked){
textView.append("\nBy uncheck : ${listenerButton.text} ")
}
}else{
textView.text = "Checked : Nothing"
if (!isChecked){
textView.append("\nBy Uncheck : ${listenerButton.text} ")
}
}
}
}
}
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"
android:background="#DCDCDC"
android:id="@+id/rootLayout"
android:padding="24dp">
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/materialButtonToggleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/color_state_material_toggle_button"
android:checkable="true"
android:text="Android" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/color_state_material_toggle_button"
android:checkable="true"
android:text="Java" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnKotlin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/color_state_material_toggle_button"
android:text="Kotlin" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnPhp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/color_state_material_toggle_button"
android:text="PHP" />
</com.google.android.material.button.MaterialButtonToggleGroup>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="TextView"
android:fontFamily="sans-serif"
android:textSize="24sp"
android:layout_marginTop="64dp"
app:layout_constraintTop_toBottomOf="@id/materialButtonToggleGroup"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/color/color_state_material_toggle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:color="#848482"/>
<item
android:state_checked="true"
android:color="#007FFF"
/>
<item
android:state_checked="false"
android:color="#89CFF0"/>
<item
android:state_enabled="true"
android:color="#F0FFFF"/>
</selector>
build.gradle dependencies
// Material components
implementation 'com.google.android.material:material:1.6.1'