Reputation: 217
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.
Upvotes: 1
Views: 786
Reputation: 337
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"/>
Upvotes: 1