Home > other >  How to add choice chip dynamically in android studio in my case
How to add choice chip dynamically in android studio in my case

Time:09-16

I have Model Class Like this:

private String animalName, type;

public Model(){}

public Model(String animalName, String type){
 this.animalName = animalName;
 this.type = type;
}
//and getter and setters for all vars

And ArrayList<Model>(); according to my model class

so I want to check if my ArrayList<>() contains a specific Type ie. herbivore then add those animals according to their type in different ArrayList and I want to make CHOICE CHIPS according to my type. here is an example of my app:

enter image description here

Note I am using RecyclerView to display Images

How Can I add That Choice Chips According to my Model Class and OnClick on any chips load shows Images according to that.

Any Solution is Appreciated.

CodePudding user response:

You can Create A Method name it like this or whatever you want

private void setChips(String type){
    Chip chip = new Chip(this);
    chip.setText(chipsTitle);
    chip.setCheckable(true);
    //if you not create chipGroup in you xml yet then create it 
    chipGroup.addView(chip);
}

And then you can call that method and pass your type for ie - "Herbivores" etc.

setChips("Herbivores");

And After that you can add ClickListeners to Your chips like this:

for (int i = 0; i < chipGroup.getChildCount(); i  ) {
     int finalI = i;
     chipGroup.getChildAt(i).setOnClickListener(v -> {
            Chip chip = chipGroup.findViewById(chipGroupFl.getChildAt(finalI).getId());
            chip.setChecked(true);

            //here you can call your method to load Images
            

        });
    }
  • Related