Friday, March 6, 2020

android kotlin - MaterialButtonToggleGroup example

MainActivity.kt

package com.example.jetpack

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <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:textAppearance="@style/TextAppearance.AppCompat.Large"
        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>
More android examples