Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getText() always return empty string from dynamically created Chip component #394

Closed
Kenc44 opened this issue Jun 12, 2019 · 6 comments
Closed

Comments

@Kenc44
Copy link

Kenc44 commented Jun 12, 2019

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.

@ymarian
Copy link
Contributor

ymarian commented Jun 13, 2019

Hi it seems the text will be in the ChipDrawable.

Does chip.getChipDrawable().getText() work?

Seems like a bug though, filed this one:
https://issuetracker.google.com/issues/135180392

@ymarian ymarian closed this as completed Jun 13, 2019
@Kenc44
Copy link
Author

Kenc44 commented Jun 13, 2019

Hi, thanks for the response. Yes it works when getting the text from the ChipDrawable instead. Just to make it more clear, it is ((ChipDrawable) chip.getChipDrawable()).getText() since getChipDrawable() seems to only return android.graphics.drawable.Drawable object which does not support getText().

@wcshi
Copy link
Contributor

wcshi commented Jun 13, 2019

Hi @Kenc44 are programmatically creating a ChipDrawable and setting it on a chip in order to programmatically specify the chip style?

Have you tried creating a theme overlay that can be passed into the chip constructor?

@Kenc44
Copy link
Author

Kenc44 commented Jun 13, 2019

Hi @wcshi I only create a ChipDrawable and setting the ChipDrawable using setChipDrawable() and didn't do anything related to theme overlay.

@wcshi
Copy link
Contributor

wcshi commented Jun 25, 2019

This should fix your issue:
8c0f6f7#diff-517b28ba515355e7ccfece81507a8cc1

@Kenc44
Copy link
Author

Kenc44 commented Jun 26, 2019

Yes that fixed the issue. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants