0

So, I make a custom View inherited from Material's Chip. Here's the View itself :

class ChipView
@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : Chip(context, attrs, defStyleAttr) {

    init {
        val chipDrawable = ChipDrawable.createFromAttributes(context, null, 0, R.style.ChipView_RP_Action)
        setChipDrawable(chipDrawable)
    }
}

So, to apply style I use either theme of app, which is not my case because I'm doing this view for a library, or I apply this style for a ChipDrawable and set it for the chip afterwards

So, here's style :

<style name="ChipView.RP" parent="Widget.MaterialComponents.Chip.Action">
    <item name="chipMinHeight">@dimen/design_chip_min_height</item>
    <item name="chipIconSize">18dp</item>
    <item name="chipMinTouchTargetSize">0dp</item>
    <item name="chipSpacingHorizontal">0dp</item>
    <item name="ensureMinTouchTargetSize">false</item>
    <item name="iconGravity">textStart</item>
    <item name="textStartPadding">8dp</item>
    <item name="textEndPadding">8dp</item>
    <item name="iconStartPadding">4dp</item>
    <item name="android:textColor">@color/black</item>
    <item name="chipIconTint">@color/black</item>
    <item name="chipBackgroundColor">@color/common_dufroid</item>
    <item name="chipCornerRadius">@dimen/design_chip_corner_radius</item>
</style>

<style name="ChipView.RP.Action"></style>

Now, if I set 2 of them in layout:

<com.example.mobileapp.widget.ChipGroup
    android:id="@+id/item"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view_header" >
    <com.example.mobileapp.widget.ChipView
        android:id="@+id/view_first_chip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chip_text_1"/>
    <com.example.mobileapp.widget.ChipView
        android:id="@+id/view_second_chip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chip_text_2"/>
</com.example.mobileapp.widget.ChipGroupView>

I got extra margins or paddings that I don't want. But if I set an icon programmatically - these paddings are gone and view looks just the way I want. Here's how I set an icon and extra paddings/margins are gone :

chip1.chipIcon = ContextCompat.getDrawable(chip2.context, R.drawable.ic18__null)

enter image description here

So, how do I get rid of that extra spacing on the second chip so it will look exactly like the first where I set icon?

user2234594
  • 305
  • 1
  • 10
  • 19

0 Answers0