I have more chip, I have add to the array, but how can display it?

I have more chip, how can add into the array and use?

I have add to the array, but how can display it?

this is my code

 String[] CampSite = new String[]{getApplicationContext().getResources().getString(R.string.campsite_wifi),
                getApplicationContext().getResources().getString(R.string.campsite_wheelchair),
                getApplicationContext().getResources().getString(R.string.campsite_water_from_tap),
                getApplicationContext().getResources().getString(R.string.campsite_toilet),
                getApplicationContext().getResources().getString(R.string.campsite_swimming_pool),
                getApplicationContext().getResources().getString(R.string.campsite_surface),
                getApplicationContext().getResources().getString(R.string.campsite_suit_any_car),
                getApplicationContext().getResources().getString(R.string.campsite_pet_welcome),
                getApplicationContext().getResources().getString(R.string.campsite_laundromat),
                getApplicationContext().getResources().getString(R.string.campsite_large_vehicle_access),
                getApplicationContext().getResources().getString(R.string.campsite_kitchen),
                getApplicationContext().getResources().getString(R.string.campsite_internet),
                getApplicationContext().getResources().getString(R.string.campsite_household_power),
                getApplicationContext().getResources().getString(R.string.campsite_hot_shower),
                getApplicationContext().getResources().getString(R.string.campsite_dump_station),
                getApplicationContext().getResources().getString(R.string.campsite_credit_card),
                getApplicationContext().getResources().getString(R.string.campsite_cellular_signal),
                getApplicationContext().getResources().getString(R.string.campsite_caravan_power),
                getApplicationContext().getResources().getString(R.string.campsite_cabin),
                getApplicationContext().getResources().getString(R.string.campsite_bed_supplied),
                getApplicationContext().getResources().getString(R.string.campsite_bbq),
        };

this is xml

<com.google.android.material.chip.ChipGroup
            android:theme="@style/CampsitePost"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

Answer

You need to loop the list CampSite and add them to the ChipGroup, set an id in your .xml

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ChipGroup chipGroup = findViewById(R.id.chip_group_main);

        for (String camp : CampSite)
        {
           Chip chip = new Chip(this);
           chip.setText(camp);
           chip.setChipBackgroundColorResource(R.color.colorAccent); 
           chip.setCloseIconVisible(true);
           chip.setTextColor(getResources().getColor(R.color.white));
           chip.setTextAppearance(R.style.ChipTextAppearance);
           chip.setId(ViewCompat.generateViewId());

           // edit:
           chip.setCheckable(true);
           chip.setOnCheckedChangeListener() // could use this per item
           chipGroup.addView(chip);
        }
}

Kepp in mind that you need to set and id (chip.setId) so you can add some functionality like getting which ones are selected

EDIT: Chip groups can use the OnCheckedChangeListener to set one callback for all chips in a chip group. This provides the ID of the chip that was selected.

ChipGroup chipGroup = (ChipGroup)findViewById(R.id.chip_group);
chipGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
 @Override
 public void onCheckedChanged(ChipGroup group, @IdRes int checkedId) {
  // Handle the chip group action.
 }
});

Leave a Reply

Your email address will not be published. Required fields are marked *