It's very strange that why fontFamily is not working on androidX .
this is my code:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="testing "
android:textSize="17sp"
android:fontFamily="@font/iransans"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
the above code doesn't effect font at all , when I convert the textview to app compact, then it works :
<androidx.appcompat.widget.AppCompatTextView
i wanted to set my whole application font but it doesn't work as I expected :
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fontFamily">@font/iransans</item>
</style>
is there any way to solve this ? How can I use my font in entire app without setting font family for each view ?
You are potentially running into 1+ of 3 things:
android:fontFamily
but not setting fontFamily
in your theme. make sure you have both:<item name="fontFamily">@font/iransans</item>
<item name="android:fontFamily">@font/iransans</item>
If you're running on older API levels, you could be running into this bug, and an upgrade to the androidx.appcompat dependency might do the trick:
https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha02
If you have any other themes defined in your styles.xml
, and the parent of your TextView
is using a separate theme, that theme may be taking precedence over the fontFamily
attribute on the view (I have not yet determined why, but this is the scenario I'm running into myself). Setting the android:theme
directly on my TextView
to a style that defines the font family did the trick.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/MyCustomFontTextAppearance" />
in styles.xml:
<style name="MyCustomFontTextAppearance" parent="TextAppearance.AppCompat.Body1">
<item name="fontFamily">@font/iransans</item>
<item name="android:fontFamily">@font/iransans</item>
</style>
I had the similar issue, when my layout looked correctly (with custom fonts applied) in IDE preview, but without fonts on real device.
The reason of the issue was that my activity was inherited from Activity
, not AppCompatActivity
. Changing to AppCompatActivity
resolved the issue without renaming all TextView
s to AppCompatTextView
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With