Monday, January 7, 2019

android kotlin - Chip group example

MainActivity.kt

package com.cfsuman.jetpack

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import com.google.android.material.chip.Chip


class MainActivity : AppCompatActivity() {

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

        // Set chip group checked change listener
        chipGroup.setOnCheckedChangeListener{group,checkedId:Int ->
            // Get the checked chip instance from chip group
            val chip:Chip? = findViewById(checkedId)

            chip?.let {
                // Show the checked chip text on toast message
                toast("${it.text} checked")
            }
        }
    }
}

// Toast extension method
fun Context.toast(message:String)=
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show()
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rootLayout"
        tools:context=".MainActivity"
        android:background="#fdfdfc">
    <com.google.android.material.chip.ChipGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/chipGroup"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="32dp"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="8dp"
            android:background="#fafff7"
            app:singleLine="true"
            app:singleSelection="true"
    >
        <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/chipRed"
                android:text="Red"
                style="@style/Widget.MaterialComponents.Chip.Choice"
        />
        <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/chipGreen"
                android:text="Green"
                style="@style/Widget.MaterialComponents.Chip.Choice"
        />
        <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/chipBlue"
                android:text="Blue"
                style="@style/Widget.MaterialComponents.Chip.Choice"
        />
        <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/chipYellow"
                android:text="Yellow"
                style="@style/Widget.MaterialComponents.Chip.Choice"
        />
        <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/chipPink"
                android:text="Pink"
                style="@style/Widget.MaterialComponents.Chip.Choice"
        />
    </com.google.android.material.chip.ChipGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
gradle dependencies

// Material components theme
implementation 'com.google.android.material:material:1.0.0'
More android examples