0

I'm trying to dynamically create some choice chip components based on an ArrayList of String from some computation and following are the code to create the chips and adding them to a ChipGroup created in layout XML file.

        if (mChipGroup.getChildCount() == 0 ){
            int i = 0;
            for (Classifier.Recognition res: results){
                Chip resultChip = new Chip(getDialog().getContext());
                ChipDrawable chipDrawable =
                        ChipDrawable.createFromAttributes(
                                getActivity(),
                                null,
                                0,
                                R.style.Widget_MaterialComponents_Chip_Choice);
                resultChip.setId(i++);
                resultChip.setChipDrawable(chipDrawable);
                resultChip.setText(res.getTitle());
                mChipGroup.addView(resultChip);
            }
        }

The Chips displayed correctly with the text but when I tried to call getText() on the chips, it always return empty String but not the text contained by the chips. I tested this by setting the OnCheckedChangeListener on the ChipGroup and making a Toast with the text (though it didn;'t work). When I tried to display only the checkedId it works.


        mChipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(ChipGroup group, int checkedId) {
                Chip chip = group.findViewById(checkedId);
                if(chip != null){
                    Toast.makeText(getContext(), chip.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            }
        });

My current workaround is to have a variable holding the array results and use ArrayList.get(selectedChipId.getTitle()). but don't think it should be that way though

I also found that it is able to get text from Chips added in layout file but not run-time added Chips. Tried with both 1.1.0/alpha06 and 1.1.0/alpha07 release but am having no luck. Would like to have some advice if possible. Thank you very much.

Kar Keng Chan
  • 73
  • 1
  • 8
  • can you get correct chips Id by clicking on them? also do you have a ChipGroup? take a look at this example: https://android--code.blogspot.com/2019/01/android-kotlin-create-chip.html – Payam Asefi Jun 12 '19 at 19:45
  • @payam hi yes I could get the correct chip id and have referred to multiple examples on getting the text but still having no luck. My current workaround is to have a variable holding the array results and use `ArrayList.get(selectedChipId.getTitle()).` but don't think it should be that way though – Kar Keng Chan Jun 13 '19 at 02:33

1 Answers1

0

So, it seems like a bug as per answered in here and here. Current workaround is to use ((ChipDrawable) chip.getChipDrawable()).getText() instead.

Kar Keng Chan
  • 73
  • 1
  • 8
  • https://github.com/material-components/material-components-android/commit/8c0f6f72c96d502cde11504f02cd5b9042fe25fd#diff-517b28ba515355e7ccfece81507a8cc1 the change is committed – Kar Keng Chan Jun 26 '19 at 15:51