user10882820
user10882820

Reputation:

Add custom font to attrs.xml

I have an android application where I make use of a custom font, let's call it my_font.ttf which I have saved in my font folder in the res package.

I also have a custom number picker where I need to set the font for the values in the picker. This is my attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NumPicker">
   
        <attr name="max" format="integer" />
        <attr name="min" format="integer" />
        <attr name="selectedTextColor" format="color" />
        <attr name="selectedTextSize" format="float" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />

        <attr name="typeface" format="enum">
            <enum name="font" value="0"/>
            <enum name="sans" value="1"/>
            <enum name="serif" value="2"/>
            <enum name="monospace" value="3"/>
        </attr>
        
    </declare-styleable>
</resources>

I want to know how I can add my_font.ttf into this attrs.xml so that I can use it in my layout file to set the font. Like those serif, sans, monospace ones work as they are built in fonts, but I am not sure how to use my font.

Here is my layout file which makes use of the custom NumPicker:

<com.myproject.slidertest.selectors.numberpicker.NumPicker
            android:id="@+id/numberPicker"
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:layout_gravity="center_horizontal"
            app:typeface="serif"<!-- I want to be able to change this font to my font-->
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:selectedTextColor="@color/color_blue"
            app:textColor="@color/colorTextDefault"
            app:max="50"
            app:min="10"
          />

Any help or advice will be highly appreciated.

Upvotes: 1

Views: 1063

Answers (1)

Lena Bru
Lena Bru

Reputation: 13947

Add another attr

<attr fontTextAppearance format="reference">

In the custom view, if the typeface type is font, look for the typefaceFont reference.

val textAppearance = ta.getResourceId(R.styleable.NumPicker_fontTextAppearance, -1)
        if (textAppearance != -1) {
            textView.setTextAppearance(textAppearance)
        }



 

 <com.myproject.slidertest.selectors.numberpicker.NumPicker
        android:id="@+id/numPicker"
        style="@style/NumPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fontTextAppearance="@style/NumPickerFont"
      />

 <style name="NumPickerFont">
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">40sp</item>
    <item name="android:textAlignment">viewStart</item>
    <item name="android:letterSpacing">0.18</item>
    <item name="android:fontFamily">@font/some_font</item>
</style>

Upvotes: 2

Related Questions