https://kotlinlang.org logo
s

Slackbot

01/21/2020, 9:46 PM
This message was deleted.
a

aipok

01/22/2020, 5:24 AM
You can define a font family and set a font weight for specific font and ally the style to the TextView with specific font family type. Check this article https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
u

ursus

01/22/2020, 5:24 PM
Not really. How can you apply weight=200 (thin) at callsite?
afaik you can only choose textStyle=regular/bold
a

aipok

01/22/2020, 5:27 PM
One way is to apply style, there you have weight parameter defined or you can define font weight from compat library... It was
app:fontWeight
or something like this.
if so, that only works for me api 28+, app:textFontWeight on android 6 does nothing
a

aipok

01/22/2020, 6:13 PM
ok, you right. I did accomplished it differently using styles.
Copy code
<style name="MyFontLight">
        <item name="fontFamily">@font/arima_madurai_light</item>
    </style>
    <style name="MyFontLightItalic">
        <item name="fontFamily">@font/arima_madurai_light</item>
        <item name="android:textStyle">italic</item>
    </style>
    <style name="MyFontMedium">
        <item name="fontFamily">@font/arima_madurai_medium</item>
    </style>
    <style name="MyFontMediumItalic">
        <item name="fontFamily">@font/arima_madurai_medium</item>
        <item name="android:textStyle">italic</item>
    </style>
    <style name="MyFontBold">
        <item name="fontFamily">@font/arima_madurai_bold</item>
    </style>
    <style name="MyFontBoldItalic">
        <item name="fontFamily">@font/arima_madurai_bold</item>
        <item name="android:textStyle">italic</item>
    </style>
    <style name="MyFontExtraBold">
        <item name="fontFamily">@font/arima_madurai_extrabold</item>
    </style>
    <style name="MyFontExtraBoldItalic">
        <item name="fontFamily">@font/arima_madurai_extrabold</item>
        <item name="android:textStyle">italic</item>
    </style>
and usage is something following
Copy code
<TextView
            style="@style/MyFontLight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/custom_font" />

    <TextView
            style="@style/MyFontLightItalic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/custom_font" />

    <TextView
            style="@style/MyFontMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/custom_font" />

    <TextView
            style="@style/MyFontMediumItalic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/custom_font" />

    <TextView
            style="@style/MyFontBold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/custom_font" />

    <TextView
            style="@style/MyFontExtraBold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/custom_font" />
result is like expected
It would be great if the compat attribute would work similar in compat library (even though I was sure I saw it working).
u

ursus

01/22/2020, 6:19 PM
yea, I also wanted styles, but my issue was that it needs to a attr, and you cannot inteherit thos if you need a style which inherits from your MyFontBold for example
youd just easily FooStyle parent=MyFontBold", but you cannot have parent="?attr/fontBoldStyle"
a

aipok

01/23/2020, 7:00 AM
I'm not sure about your exact need, but if you just need to override some fonts you can use
ThemeOverlay
theme for this and define fonts via it. In this case you will be able to use regular
style="@style/***"
for any other adjustments you need.
2 Views