material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[ChipGroup] Chip with the type of Choice can't be marked/checked programmatically

Open ikhsanhabibi opened this issue 3 years ago • 4 comments
trafficstars

Description: I tried to retrieve data from the last state, I want to mark a chip (type Choice where app:singleSelection="true") in the ChipGroup

Expected behavior: the color should be changed, the chip is marked

Source code: The code snippet which is causing this issue I tried to use chg.checked(chip.id) it doesn't work.

  private fun restoreLastValue() {
        val sharedPreferences = getPreferences(MODE_PRIVATE)
        val rentOrBuy = sharedPreferences.getString("rentOrBuy", "")
        val view = layoutInflater.inflate(R.layout.object_type_dialog, null)
        val chg = view.findViewById<ChipGroup>(R.id.chipGroup1)
        val chipsCount = chg.childCount

        if (rentOrBuy != ""){
            var i = 0
            while (i < chipsCount) {
                val chip = chg.getChildAt(i) as Chip
                val chipText = chip.text.toString()
                if (chipText == rentOrBuy.toString()){
                    chg.check(chip.id)
                    chip.isChecked = true
                    chg.setSingleSelection(chip.id)
                }
                i++
            }
        } else {

        }

    }

Minimal sample app repro: Please consider attaching a minimal sample app that reproduces the issue. This will help narrow down the conditions required for reproducing the issue, and it will speed up the bug fix process. You may attach a zip file of the sample app or link to a GitHub repo that contains the sample app.

Android API version: Android API version here

android {
    compileSdkVersion 32
    defaultConfig {
        applicationId "com.corebuild.arlocation.demo"
        minSdkVersion 24
        targetSdkVersion 32
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

Material Library version: Material Android Library version you are using here (e.g., 1.1.0-alpha07) implementation 'com.google.android.material:material:1.6.1'

Device: Device on which the bug was encountered here Samsung S10

Please, can someone help me??? :)

ikhsanhabibi avatar Sep 05 '22 15:09 ikhsanhabibi

Well, now I understand, hahaha.

Programmatically to select a chip (Choice type, single selection) in the ChipGroup => deselect all chips c.isSelected = false then select a chip chip.isSelected = true

For chip selection (type Filter, more selection possible) => chip.isChecked = true

ikhsanhabibi avatar Sep 06 '22 13:09 ikhsanhabibi

I am bothered by this behavior.

yuchan2215 avatar Sep 06 '22 15:09 yuchan2215

https://github.com/material-components/material-components-android/blob/4c7be527751f1cb11f7c5f55ca5eecbb41c1620c/lib/java/com/google/android/material/internal/CheckableGroup.java#L155

https://github.com/material-components/material-components-android/blob/4c7be527751f1cb11f7c5f55ca5eecbb41c1620c/lib/java/com/google/android/material/internal/CheckableGroup.java#L159

  private boolean checkInternal(@NonNull MaterialCheckable<T> checkable) {
    int id = checkable.getId();
    if (checkedIds.contains(id)) {
      return false;
    }
    MaterialCheckable<T> singleCheckedItem = checkables.get(getSingleCheckedId());
    if (singleCheckedItem != null) {
      uncheckInternal(singleCheckedItem, false);
    }
    boolean checkedStateChanged = checkedIds.add(id);
    if (!checkable.isChecked()) {
      checkable.setChecked(true);
    }
    return checkedStateChanged;
  }

I found the implementation and will make a note of it.

yuchan2215 avatar Sep 06 '22 15:09 yuchan2215

Is this still an issue?

drchen avatar Sep 12 '22 19:09 drchen

I'll close this issue for now due to no further info available. Please feel free to reopen it if this is still happening.

drchen avatar Sep 29 '22 20:09 drchen