Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the typeface of Chip view?

I would like to change the typeface of Chip view from design support library. After I explorer it's source code, I found that it draw the text in ChipDrawable directly by Paint and I can't find any public method to change the typeface. How should I try to change that Chip view typeface? Please help. Thanks you.

like image 369
BomberBus Avatar asked Jun 12 '18 12:06

BomberBus


2 Answers

It's a little late but I will post my solution in case someone is still interested.

First create a font family xml with your custom font.

<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/my_custom_font"/>
</font-family>

Then add a chip style to your styles xml that uses your font family:

<style name="chipText" parent="TextAppearance.MaterialComponents.Chip">
    <item name="android:textSize">14sp</item>
    <item name="android:fontFamily">@font/my_font_family</item>
</style>

Then, when you create your chip view in xml, set the textAppearance attribute:

android:textAppearance="@style/chipText"
like image 167
foolioJones Avatar answered Nov 10 '22 21:11

foolioJones


your solution working normal chip view - it still has some issue chip inside TextInputLayout. (font not changing)

notes: after screen rotates custom font changed, I don't know why

implementation "com.google.android.material:material:1.3.0-alpha01"

   <com.google.android.material.textfield.TextInputLayout
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_gravity="center"
        android:textAppearance="@style/chipText"
        android:hint="Text Field">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Text"
            android:textAppearance="@style/chipText"/>

    </com.google.android.material.textfield.TextInputLayout>
like image 1
Hari Shankar S Avatar answered Nov 10 '22 19:11

Hari Shankar S