Native Mobile App Development With Java

Android Multi Select Spinner example Java

13-Oct-2024

Android Multi Select Spinner example Java


1/  Create A New Project  and design the page


<?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_alignParentRight="true"
android:src="@drawable/baseline_arrow_drop_down_24" />

</RelativeLayout>

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


2/  Java Activity


package com.tutorialb.multispinner;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.material.card.MaterialCardView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

MaterialCardView cardv;
TextView tvitem;
ArrayList<Integer> arrayList = new ArrayList<>();
String[] arrayitem = {"book", "pensil", "pen", "sheet"};
boolean[] selectitem;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

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

selectitem = new boolean[arrayitem.length];

cardv.setOnClickListener(v -> {

ShowAltItem();
});
}

public void ShowAltItem() {

AlertDialog.Builder bilder = new AlertDialog.Builder(this);
bilder.setTitle("Select Item");
bilder.setCancelable(false);

bilder.setMultiChoiceItems(arrayitem, selectitem, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {

if (isChecked) {

arrayList.add(which);
} else {

arrayList.remove(which);
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
}).setNeutralButton("Clear All", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});

bilder.show();
}
}



3/ Ran And Output



Comments