Android Multi Select Spinner example kotlin

29-Dec-2024

Android Multi Select Spinner example kotlin

Output:







1/  Modify the activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>

<com.google.android.material.card.MaterialCardView
android:id="@+id/cardv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvitem"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="Select the item "
android:textSize="15sp"
android:textStyle="bold|italic" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:src="@drawable/baseline_arrow_drop_down_circle_24" />

</RelativeLayout>

</com.google.android.material.card.MaterialCardView>
</RelativeLayout>



2 /Modify the MainActivity.java



package com.zissofworks.multiselectspinner

import android.app.AlertDialog
import android.os.Bundle
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.material.card.MaterialCardView


class MainActivity : AppCompatActivity() {
lateinit var cardv: MaterialCardView
lateinit var tvitem: TextView
var arrayList: ArrayList<Int> = ArrayList()
var arrayitem: Array<String> = arrayOf("book", "pensil", "pen", "sheet")
lateinit var selectitem: BooleanArray

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

selectitem = BooleanArray(arrayitem.size)

cardv = findViewById(R.id.cardv)
tvitem = findViewById(R.id.tvitem)


cardv.setOnClickListener {
showAltItem();
}
}

private fun showAltItem() {

val builder = AlertDialog.Builder(this)
builder.setTitle("Select Item")
builder.setCancelable(false)

builder.setMultiChoiceItems(arrayitem, selectitem) { _, which, isChecked ->
if (isChecked) {
arrayList.add(which)
} else {
arrayList.remove(which)
}
}
.setPositiveButton("OK") { _, _ ->
}
.setNegativeButton("Cancel") { _, _ ->
}
.setNeutralButton("Clear All") { _, _ ->
}

builder.show()
}
}


Comments