I'm struggling with my custom Toolbar
, in particular with its font style. The font family inherited of the toolbar title isn't the expected one (the one set for the app), but it's the default android "Roboto".
My xml layout:
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/ctlArticleHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fitsSystemWindows="true"
app:collapsedTitleTextAppearance="@style/ToolbarCollapsed"
app:expandedTitleTextAppearance="@style/ToolbarExpanded"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="bottom"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:statusBarScrim="@android:color/transparent"
app:toolbarId="@+id/tbNavigation">
<ImageView
android:id="@+id/ivSectionImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
tools:src="@tools:sample/avatars" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/tbNavigation"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="start"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_collapseMode="pin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways"
tools:title="Lorem ipsum"
app:navigationIcon="@drawable/ic_arrow_back_white">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
My styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:fontFamily">@font/spoof</item>
<item name="actionMenuTextColor">@color/colorPrimary</item>
<item name="actionMenuTextAppearance">@style/myCustomMenuTextAppearance</item>
<item name="android:textColor">@color/colorBaseBlack</item>
</style>
</resources>
Is that an expected behavior? Everything is working fine for the rest of the app. Am I supposed to force the font family in a dedicated style for the toolbar and duplicating the code?
In the end I had to set font type programmatically from my fragment. Setting the style from xml helped with the layout preview (working as expected), however it doesn't work at runtime.
So, in my fragment I have:
ctlArticleHeader.setCollapsedTitleTypeface(ResourcesCompat.getFont(requireContext(), R.font.spoof))
ctlArticleHeader.setExpandedTitleTypeface(ResourcesCompat.getFont(requireContext(), R.font.spoof))
By doing that the font is "forced" programmatically and it's correctly applied in my layout