Is there a way to set the font for a CollapsingToolbarLayout
?
I'm using Calligraphy but my default font is not applied.
I think the problem is the CollapsingTextHelper
class is using Canvas.drawText()
instead of a TextView
.
How can I change the default font that is used for Canvas.drawText()
?
You can change the text appearance of the title of a CollapsingToolbarLayout as follows:
1) Make sure that you are using
compile 'com.android.support:design:22.2.1'
in your build.gradle dependencies.2) Use expandedTitleTextAppearance like this:
3) Define the HeaderTitleStyle in your styles file:
Custom font with the Android App Bar Layout and Collapsing Toolbar Layouts
Inside of CoordinatorLayout below code:
Style:
Output:
First step is to pick a font that you want to use.
Second create a Fonts folder in your assets directory and copy your font there.
Xml file
Java File
Since one of the latest API updates, the Support Design Package has been updated and now it's possible to set the font of the collapsing title text.
Use
setCollapsedTitleTypeFace(Typeface typeface)
andsetExpandedTitleTypeFace(Typeface typeface)
to set your custom font.You can trick by wrapping the widget class. Change the
TypeFace
when widget attached to window.Tip:
Calligraphy
library included helper class forTypeFace
. You can generateTypeFace
based onAssetManager
and path to font in assets directory.For example, I will apply custom font for
CollapsingToolbarLayout
class:And in layout xml file, use
CollapsingToolbarLayoutWrapper
instead:Result
It is possible. In fact, by default, the font of the title when it's collapsed is different than the one when it's not.
So, in order to change this, for example, you can do as such:
styles file
layout file
Similar thing can be done for the style of when it's not collapsed ("expandedTitleTextAppearance") .