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());
}
}
}
});
}