Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: set default choice chip

Is there any way to set a choice chip as a default option? Furthermore, not to be able to uncheck any chip if there is only one chip selected?

like image 230
Hawklike Avatar asked Dec 10 '22 02:12

Hawklike


1 Answers

You can use these attributes in the ChipGroup component:

  • app:singleSelection="true". In this way the ChipGroup can be configured to only allow a single chip to be checked at a time using
  • app:checkedChip to define a default choice

Something like:

        <com.google.android.material.chip.ChipGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:singleSelection="true"
            app:checkedChip="@id/ch2">

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ch1"
                android:text="M"/>

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ch2"
                android:text="F"/>

        </com.google.android.material.chip.ChipGroup>
like image 101
Gabriele Mariotti Avatar answered Jan 07 '23 21:01

Gabriele Mariotti