Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 722
Lorenzo Marchesi Avatar asked Jul 17 '18 08:07

Lorenzo Marchesi


People also ask

How do I change the color of the selected chip in Android?

So you can use the setChipBackgroundColor(ColorStateList cl) method to set the color of your chip and then you can add an setOnClickListener(new ...) to toggle with selection and non-selection like the following code: yourchip. setOnClickListener(new View.

How do you color text on Android?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

How do we change a color of a text in Android Studio?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .


2 Answers

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>
like image 104
hollmannlu Avatar answered Nov 10 '22 22:11

hollmannlu


You can simply use android:textColor in xml like:

    <com.google.android.material.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"/>

Or programatically you can use:

val chip = view.findViewById<Chip>(R.id.chip)
chip.setTextColor(Color.WHITE)
like image 43
Adrian Le Roy Devezin Avatar answered Nov 10 '22 21:11

Adrian Le Roy Devezin