Reputation: 1668
I search a lot on internet but couldn't find the exact solution. Here is the link that i last tried from SO. Get selected Chips from a ChipGroup
I want to get selected chips from chip group when a button is clicked.
This function contain information of displaying names in RecyclerView
private void getNames() {
List<String> names = Arrays.asList(getResources().getStringArray(R.array.names));
int count = 0;
for ( String name : names){
list.add(new Names(name));
count++;
}
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
namesAdapter = new NamesAdapter(MainActivity.this, list);
recyclerView.setAdapter(namesAdapter);
}
When click on RecyclerView item one chip is added into the ChipGroup here is the function
public void onItemSelected(Names name) {
Chip chip = new Chip(this);
chip.setText(name.getName());
chip.setCloseIconVisible(true);
chip.setCheckable(false);
chip.setClickable(false);
chip.setOnCloseIconClickListener(this);
chipGroup.addView(chip);
chipGroup.setVisibility(View.VISIBLE);
}
This is the image of displaying chips in ChipGroup
Function that is getting values from ChipGroup
public void getChipGroupValues(){
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ChipGroup chipGroup = findViewById(R.id.chipGroup);
for (int i=0; i<chipGroup.getChildCount();i++){
Chip chip = (Chip)chipGroup.getChildAt(i);
Log.i("outside if ", i+ " chip = " + chip.getText().toString());
if (chip.isChecked()){
Log.i("inside if ", i+ " chip = " + chip.getText().toString());
textView.setText(chip.getText().toString());
}
}
}
});
}
build.gradle(Module.app) detail
Upvotes: 14
Views: 27661
Reputation: 11
If you know the id of select chip then check as
if (binding.chipGroup.checkedChipId == binding.chipOtherTip.id)
{
showMessage(getString(R.string.tips_amount), binding.root, Color.RED)
return@setOnClickListener
}
Upvotes: 0
Reputation: 36
chip_group.setOnCheckedStateChangeListener(new ChipGroup.OnCheckedStateChangeListener() {
@Override
public void onCheckedChanged(@NonNull ChipGroup group, @NonNull List<Integer> checkedIds) {
List<Integer> ids = group.getCheckedChipIds();
for (Integer id:ids){
Chip chip = group.findViewById(id);
Toast.makeText(BottomActivity.this, chip.getText(), Toast.LENGTH_SHORT).show();
}
}
});
Upvotes: 1
Reputation: 1055
To get selected chip's text from chipgroup, you can use this one liner in Kotlin:
val selectedChipText = parentChipGroup.findViewById<Chip>(parentChipGroup.checkedChipId).text.toString()
Upvotes: 3
Reputation: 11100
the solution I used in Kotlin with data binding
mBinding?.chipGroup?.children
?.toList()
?.filter { (it as Chip).isChecked }
?.forEach { //here are your selected chips as 'it' }
And here is how I got just titles
mBinding?.chipGroup?.children
?.toList()
?.filter { (it as Chip).isChecked }
?.joinToString(", ") { (it as Chip).text }
Upvotes: 13
Reputation: 363865
Starting with the version 1.2.0 you can use the method chipGroup.getCheckedChipIds()
List<Integer> ids = chipGroup.getCheckedChipIds();
for (Integer id:ids){
Chip chip = chipGroup.findViewById(id);
//....
}
OLD ANSWER with 1.1.0:
currently there isn't a direct API to get the selected chips.
However you can iterate through the children of the ChipGroup
and check chip.isChecked()
.
ChipGroup chipGroup = findViewById(R.id.....);
for (int i=0; i<chipGroup.getChildCount();i++){
Chip chip = (Chip)chipGroup.getChildAt(i);
if (chip.isChecked()){
//this chip is selected.....
}
}
Upvotes: 30
Reputation: 268
I used below mentioned method to detect the selected chips in chipGroup.
for (chipTitle in stringList) {
val chip = Chip(chipGroup.context)
chip.text = chipTitle
chip.tag = chipTitle
chip.isClickable = true
chip.isCheckable = true
chip.setOnCheckedChangeListener { _, isChecked ->
if (isChecked){
Log.e(TAG, chipTitle)
}
}
chipGroup.addView(chip)
}
chipGroup.isSingleSelection = true
Upvotes: 2