3

This is my code for setting up the chipgroup and chips inside based on list:

val chipGroup = region_list
                val inflator = LayoutInflater.from(chipGroup.context)
                val children = categoryList.map { categoryName ->

                    val chip = inflator.inflate(R.layout.region, chipGroup, false) as Chip
                    chip.text = categoryName.replace('_', ' ')
                    chip.tag = categoryName
                    chip.setOnCheckedChangeListener { button, isChecked ->
                        val s = viewModel.updateFilter(button.tag as String, isChecked)

//s is a string = {"change", "keep"}
//if s is keep that means that the same chip that was selected was pressed again, and in normal chip behaviour in android that chip would then be deselected, I don't want that
                    }

                }

s is a string = {"change", "keep"} if s is keep that means that the same chip that was selected was pressed again, and in normal chip behaviour in android that chip would then be deselected, I don't want that. I can't believe that android team never thought that someone would need a chipgroup where an item can't be deselected, what is the point of chips, to use it as some kind of filter to update view bellow.

Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
Alen
  • 949
  • 3
  • 17
  • 37
  • Have you tried filter-chips? – Sumit Shukla Sep 01 '19 at 15:57
  • Filter and Choice chips, same thing, if you press it once it is selected, if you press it second time it is deselected, I tried also with setting clickListener on chipGroup instead on individual element but it is the same – Alen Sep 01 '19 at 16:22
  • You can try yourself some logic if selected chip is selected! – Sumit Shukla Sep 01 '19 at 16:24
  • chipGroup.setOnCheckedChangeListener { group, selectedId -> If already selected chip is pressed again, selectedid will be -1, so that tells me that already seelcted chip is pressed, but there is NO way from preventing deselection to happen, I tried to use return statement if selectedid is -1 but it is already deselected here, there must be an easier way to do this or android people who made this are not that smart because it is a common behaviour to have chips which can not be deselected } – Alen Sep 01 '19 at 16:41

1 Answers1

0

Try this logic:

Your Activity:

    int i=0;
    String chipArr[]={"First","Second","Third","Fourth"};
    int selectedChipId=0;

    for (final String chipName : chipArr) {
         Chip chip = (Chip) layoutInflater.inflate(R.layout.cat_chip_group_item_filter, chipGroup, false);
            chip.setText(chipName);
            chip.setId(i);
    }

    for (final String chipName : fileNameChip) {
         LayoutInflater layoutInflater = getLayoutInflater();
         Chip chip = (Chip) layoutInflater.inflate(R.layout.cat_chip_group_item_filter, chipGroup, false);
         chip.setText(chipName);
         chip.setId(i);

        if (i == 0) chip.setSelected(true);

     chip.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                   selectedChipId=v.getId();
             }
             });
        chipGroup.addView(chip);
        i++;
    }

     chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(ChipGroup group, int checkedId) {
                     Log.d(TAG, "onCheckedChanged: " + checkedId);

                     if (checkedId == selectedChipId) {
                              chip.setSelected(true);
                     }
               }
    });

Activity layout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.ShowCodeActivity">

    <HorizontalScrollView
        android:id="@+id/horScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:scrollbars="none">

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chip_group_code_files"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_4dp"
            android:layout_marginEnd="@dimen/margin_4dp"
            app:singleLine="true"
            app:singleSelection="true" />

    </HorizontalScrollView>


</RelativeLayout>

Your cat_chip_group_item_filter.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Widget.MaterialComponents.Chip.Filter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Let me know if it worked for you or not as I haven't tested it!!

Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57