Search code examples
javaandroidcolorsandroid-chips

How to set text color in android.support.design.chip.Chip?


<android.support.design.chip.Chip
     style="@style/Widget.MaterialComponents.Chip.Choice"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_margin="4dp"
     android:checked="true"
     android:padding="4dp"
     android:textAppearance="?android:textAppearanceSmall"
     app:chipBackgroundColor="@color/selector"
     app:chipText="Test"
     app:chipIconEnabled="true"
     app:chipIconSize="20dp" />

In XML there is no attribute like:

chipTextColor

Programmatically SpannableString not working:

SpannableStringBuilder builder = new SpannableStringBuilder();
String numeric = getString(R.string.patient_list_order_date);
SpannableString whiteSpannable= new SpannableString(numeric);
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, numeric.length(), 0);
builder.append(whiteSpannable);
chip.setChipText(builder);

Solution

  • I managed to set the text-color in 28.0.0-alpha3 like this:

    chip.setChipText(text);
    chip.setTextAppearanceResource(R.style.ChipTextStyle_Selected);
    

    and create a style with these parameters:

    <style name="ChipTextStyle_Selected">
        <item name="android:textSize">13sp</item>
        <item name="android:textColor">@color/white</item>
    </style>