1

AndroidManifest.xml

android:theme="@style/AppTheme"

styles.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:fontFamily">@font/montserrat</item>
    <item name="fontFamily">@font/montserrat</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include layout="@layout/content_dashboard" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/background_top_right_radius_white"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/nav_header">

        <ExpandableListView
            android:id="@+id/expandableListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_160sdp"
            android:divider="@color/white"
            android:dividerHeight="0dp"
            android:groupIndicator="@null" />

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

content_dashboard.xml (Only sharing the textview code)

<TextView
    android:id="@+id/tvDashboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/_45sdp"
    android:layout_marginTop="@dimen/_10sdp"
    android:text="@string/dashboard"
    android:textColor="@color/black"
    android:textSize="@dimen/_20sdp"
    android:textStyle="bold" /> //this is not working

Dashboard.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    TextView tvDashboard = findViewById(R.id.tvDashboard);
    tvDashboard.setTypeface(null, Typeface.BOLD); //this is not working
}

The entire app font has been set to Montserrat using https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts#via-android-studio. However, in some parts I have headings and such so I would like to change the style to bold. It is working in some places when I'm using the setTypeface code, but it isn't working in other places (including the above example). That is odd and I'm not sure why it is behaving this way.

Abd Maj
  • 197
  • 1
  • 13
  • Will this resolve your problem https://stackoverflow.com/questions/16993904/android-setting-textview-to-bold-not-working – Android_id Apr 09 '20 at 15:58
  • I came across that link and if I have to change my font to default then I'll have to change the font of all views in my app manually, which is a tedious process. – Abd Maj Apr 09 '20 at 16:08

1 Answers1

1

To create a font family, perform the following steps in the Android Studio:

Right-click the font folder and go to New > Font resource file. The New Resource File window appears. Enter the file name, and then click OK. The new font resource XML opens in the editor. Enclose each font file, style, and weight attribute in the element. The following XML illustrates adding font-related attributes in the font resource XML:

<?xml version="1.0" encoding="UTF-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
   <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular" />
   <font app:fontStyle="bold" app:fontWeight="400" app:font="@font/myfont-Bold" />
   <font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" />
</font-family>

Now use it in textview as

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:fontFamily="@font/font-family"/>
Tushar Pingale
  • 337
  • 2
  • 7
  • 18
  • This will change the font to default for all views in the app, and I would like it to remain Montserrat which is the preferred font for my app. – Abd Maj Apr 09 '20 at 16:11
  • have you checked without font family? Does the text style apply without font family? – Tushar Pingale Apr 09 '20 at 16:16
  • Without font family it works, but I want to avoid that because I'll have to change the font for every single view in my app manually. There has to be a solution to fix the above without me having to get rid of fontFamily from AppTheme. What is weird is that my approach is working in some places, but not in others. I can't figure out why. – Abd Maj Apr 09 '20 at 16:19