Skip to content Skip to sidebar Skip to footer

Get Selected Chips From A Chipgroup

I'm new to working with Chips in Android. I want to get the selected Chips from a ChipGroup when I click a button. Made is someway work with checking every Chip and add it to a Se

Solution 1:

Here is my solution:

  1. just for test results, I´ve added a button in XML
<Button
  android:id="@+id/bShow"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@id/chipGroup"
  android:layout_margin="10dp"
  android:text="Show Checked"  
  android:onClick="buttonPressed"/>
  1. Java: just for the test, be sure you include:
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
  1. Java: add the following code
showResult = (Button) findViewById(R.id.bShow);
showResult.setOnClickListener(newView.OnClickListener() 
{
    @OverridepublicvoidonClick(View v) {
        Stringmsg="Chips checked are:";
        ChipGroupchg= findViewById(R.id.chipGroup);
        intchipsCount= chg.getChildCount();
        if (chipsCount == 0) {
            msg += " None!!";
        } else {
            inti=0;
            while (i < chipsCount) {
              Chipchip= (Chip) chg.getChildAt(i);
              if (chip.isChecked() ) {
                msg += chip.getText().toString() + " " ;  
              }
              i++;
            };  
        }
        // show message
        Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
    }
});

Solution 2:

For those looking for the Kotlin equivalent, it looks much simpler. This version uses Sequence as well, so its more efficient than the usual Java counterparts:

val chipGroup = // get the ChipGroup view via your favorite inflation mechanism (view bindind, android extensions, etc)val selectedChips = chipGroup.children
                .filter { (it as Chip).isChecked }
                .map { (it as Chip).text.toString() }

Solution 3:

ChipGroup does not have any method to return multiple selected chips. So you can use the traditional way by looping all child inside the parent and check whether it is checked or not.

for (index in 0until mBinding.bookingIncludeCg.childCount) {
      val chip:Chip = mBinding.bookingIncludeCg.getChildAt(index) as Chip
          when(chip.id) {
              R.id.free_cancellation_chip -> hotelFilter.freeCancellation = chip.isChecked
              R.id.free_breakfast_chip -> hotelFilter.freeBreakfast = chip.isChecked
              R.id.free_wifi -> hotelFilter.freeWifi = chip.isChecked
          }
 }

And the XML

<android.support.design.chip.ChipGroup
        android:id="@+id/booking_include_cg"
        android:layout_marginTop="@dimen/spacing_16"
        app:layout_constraintTop_toBottomOf="@+id/booking_include_label"
        app:layout_constraintLeft_toLeftOf="@id/guidelineLeft"
        app:layout_constraintRight_toRightOf="@id/guidelineRight"
        android:layout_width="0dp"
        android:layout_height="wrap_content">

        <android.support.design.chip.Chip
            android:id="@+id/free_cancellation_chip"
            style="@style/HotelFilterChipStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:chipText="@string/free_cancellation" />

        <android.support.design.chip.Chip
            android:id="@+id/free_breakfast_chip"
            style="@style/HotelFilterChipStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:chipText="@string/free_breakfast" />

        <android.support.design.chip.Chip
            android:id="@+id/free_wifi"
            style="@style/HotelFilterChipStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:chipText="@string/free_wifi" />

    </android.support.design.chip.ChipGroup>

Post a Comment for "Get Selected Chips From A Chipgroup"