0

so I have this extension function which is supposed to create a chip. I want the chip to be smaller than normal.

fun createChipFromTrait(context: Context, trait: Trait): Chip {

    val chip = Chip(context)
    if (trait.probability != null) {
        chip.text = context.getString(
            R.string.hetProbability,
            (trait.probability!! * 100).toInt(),
            trait.toFormattedString()
        )
    } else {
        chip.text = trait.toFormattedString()
    }
    if (trait.mutationFirstGene != null && trait.mutationSecondGene != null) {
        chip.chipBackgroundColor = context.getColorStateList(R.color.colorSuper)
        chip.setTextAppearanceResource(R.style.ChipText_Super)
    } else if (trait.mutationFirstGene != null || trait.mutationSecondGene != null) {
        chip.chipBackgroundColor = context.getColorStateList(R.color.colorSingleMutation)
        chip.setTextAppearanceResource(R.style.ChipText_Codom)
    }
    chip.textSize = 12f

    chip.setEnsureMinTouchTargetSize(false)

    chip.setPadding(
        0,
        0,
        0,
        0,
    )

    chip.minimumWidth = 0
    chip.minWidth = 0

    chip.minHeight = 0
    chip.minimumHeight = 0
    chip.chipMinHeight = 0f

    return chip
}

Problem visualized

As you can see, the padding or something after and before the text is wrong. The chip is correctly scaling vertically, but the width doesn't resize correctly.

Do someone has some idea about why this doesn't work?

Thank you in advance.

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Lorenzo Benevento
  • 522
  • 1
  • 3
  • 16

1 Answers1

0

Okay so, I figured it out. You have to set all these parameters to really shrink it down:

chip.setEnsureMinTouchTargetSize(false)

chip.setPadding(
    5,
    5,
    5,
    5,
)
chip.textStartPadding = 5f
chip.textEndPadding = 5f
chip.chipEndPadding = 0f
chip.chipStartPadding = 0f

chip.minWidth = 0

chip.minHeight = 0
chip.chipMinHeight = 0f
Lorenzo Benevento
  • 522
  • 1
  • 3
  • 16
  • are you sure this works? these settings didn’t work for me – MMK Jun 20 '21 at 16:59
  • Yeah they did for me! What are you trying to achieve? – Lorenzo Benevento Jun 23 '21 at 22:42
  • I want to add vertical spacing b/w chips and I want my chip to have a fixed height. take a look at [this github issue](https://github.com/material-components/material-components-android/issues/1004#issuecomment-864582134) – MMK Jun 24 '21 at 13:40